JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > React >

Nothing was returned from render error in React

Author:JIYIK Last Updated:2025/03/16 Views:

The “Nothing was returned from render” React error occurs when we forget to explicitly return a value from a function or class component. To fix the error, explicitly return JSX from our component, use implicit returns with arrow functions, or return null if we don’t intend to return anything.

Here is the sample code that produces the above error

// ⛔️ 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 use an arrow function to define the component, but use curly braces instead of parentheses, so we don’t return anything from the App component.

When a function doesn't return a value, it returns undefined, which results in an error.

To fix this, make sure to return JSX from our component.

const App = () => {
  return <div>Hello world</div>;
};

export default App;

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

If we intend to return anything from our component, we should return null.

const App = () => {
  return null;
};

export default App;

If our component performs some logic and does not need to render anything, make sure to return null, as implicitly or explicitly returning undefined will result in errors.

If you want to use implicit returns with arrow functions, make sure to use parentheses instead of curly braces.

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

The above example shows how to use implicit returns with arrow functions.

请注意, we use parentheses to wrap JSX code instead of curly braces.

We may also get an error if we explicitly use a return statement but put the brackets on the next line.

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

To fix this, put the opening bracket on the same line as the return statement.

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

If your opening bracket is placed on the next line, the engine thinks it is a return; followed by some unrelated code on the next line.

Therefore, we end up returning undefined, which results in a "Nothing was returned from render" error.

React components are just functions, so when we don’t return a value from a function, undefined is implicitly returned .

React considers null return values ​​ok, but if our function or class component returns undefined, it will throw an error.

Summarize

The “Nothing was returned from render” React error occurs when we forget to explicitly return a value from a function or class component. To fix the error, explicitly return JSX from your component, use implicit returns with arrow functions, or return null if you don’t intend to return anything.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

React Tutorial - Transferring Props

Publish Date:2025/03/16 Views:185 Category:React

React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.

React Tutorial: Props Anti-Pattern

Publish Date:2025/03/16 Views:183 Category:React

React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.

React Tutorial - Props Validation

Publish Date:2025/03/16 Views:99 Category:React

Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.

Why do you need to bind event handlers in React Class Components?

Publish Date:2025/03/16 Views:58 Category:React

When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:

Solution to the error "does not contain a default export" in React

Publish Date:2025/03/16 Views:187 Category:React

When we try to use `default import` to import from a module that does not have a `default export`, we get a "does not contain a default export" error. To fix the error, make sure the module has named exports and wrap the import in curly braces, e.g.

Solve the Module not found: Can't resolve 'react-bootstrap' error

Publish Date:2025/03/16 Views:85 Category:React

To resolve the error "Module not found: Error: Can't resolve 'react-bootstrap'", make sure to install the react-bootstrap package by opening a terminal in the root directory of the project and running the command `npm install react-bootstrap bootstrap

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial