Solve - Nothing was returned from render Error in React

avatar

Borislav Hadzhiev

Fri Mar 04 20222 min read

banner

Photo by Cerqueira

Solve - Nothing was returned from render Error in React #

The "Nothing was returned from render" React error occurs when we forget to explicitly return a value from a function or class component. To solve the error, explicitly return JSX from your component, use implicit return with an arrow function or return null if you meant to return nothing.

nothing returned from render react

Here is an example of how the error occurs.

App.js
// ⛔️ Error: Error: App(...): Nothing was returned // from render. This usually means a return statement is missing. const App = () => { <div>Hello world</div>; // 👈️ forgot to use `return` }; export default App;

We used an arrow function to define a component, but used curly braces instead of parenthesis, so we returned nothing from the App component.

When a function doesn't return a value, it returns undefined, which causes the error.

To solve this, make sure to return the JSX from your component.

App.js
const App = () => { return <div>Hello world</div>; }; export default App;

The example above uses a return statement to explicitly return some JSX from a function component.

If you meant to return nothing from the component, you should return null instead.

App.js
const App = () => { return null; }; export default App;

If your component performs some logic and does not need to render anything, make sure to return null, because implicitly or explicitly returning undefined causes the error.

If you want to use an implicit return with an arrow function, make sure to use parenthesis instead of curly braces.

App.js
const App = () => ( <div> <div>Hello world</div> </div> );

The example above shows how to use an implicit return with an arrow function. Note that we use parenthesis to wrap the JSX code and not curly braces.

You might also get the error if you explicitly use a return statement, but put the parenthesis on the next line.

App.js
function App() { return ( // 👈️ parenthesis should be on same line with return() <div> <div>Hello world</div> </div> ) } export default App;

To solve this, place the parenthesis on the same line as your return statement.

App.js
function App() { return ( <div> <div>Hello world</div> </div> ); } export default App;

If your starting parenthesis is placed on the next line, the engine considers it as return; and then some unrelated code on the next line.

So, you end up returning undefined, which causes the "Nothing was returned from render" error.

React components are just functions, so when you don't return a value from a function, you implicitly return undefined.

React considers a null return value okay, but if your function or class components return undefined, an error is thrown.

Use the search field on my Home Page to filter through my more than 1,000 articles.