0

I have a component in React which I am importing in index.js, but it is giving this error: Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null

import React, { Component } from 'react';
import './App.css';

const Intro = (props) => {
  <p className="App-intro">
    To get started, edit <code>src/App.js</code> and save to reload
  </p>
}
class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">

          <h1 className="App-title">TV Series list</h1>
        </header>
        <Intro />
      </div>
    );
  }
}

export default App;

3 Answers 3

1

Your code:

const Intro = (props) => {
  <p className="App-intro">
    To get started, edit <code>src/App.js</code> and save to reload
  </p>
}

Doesn't have a return statement.

If you wrap it within brackets {} then you need to have a return statement within the component. You don’t have to do this if it’s wrapped in parentheses though, because the syntax implies a return

Also, you’re not exporting Intro

Sign up to request clarification or add additional context in comments.

5 Comments

yep, this to :)
const Intro = (props) => ( <p className="App-intro"> To get started, edit <code>src/App.js</code> and save to reload </p> ); works though
You don't have to when used in same file, just if you want to reach it externally
Yes it does, the return is implied in an arrow-function if you use that syntax. You can't do any logic with that method though, only what's inside the return.
That makes sense, if he executes it inside {} it’s a completely different syntax though, I think that would cause an error yeah? The server would be expecting a return statement inside of that, whereas if it’s just () the return is already implied
1
const Intro = (props) => (
  <p className="App-intro">
    To get started, edit <code>src/App.js</code> and save to reload
  </p>
)

OR

const Intro = (props) => {
  return (<p className="App-intro">
    To get started, edit <code>src/App.js</code> and save to reload
  </p>);
}

Here in your code you have used {} in arrow function body but it does not return anything, hence no element/component is rendering. You can return the value from arrow function using any of the above method. Here () assumes that the only statement in body is return statement while {} requires explicit return statement.

Probably your error statement is because Intro is one stateless component.

Comments

0

If your file is called index.js as you said, that's probably the error.

You have jsx in your code therefore your file must be named: index.jsx

:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.