Categories
JavaScript Answers

How to fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript?

Spread the love

Sometimes, we want to fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript.

In this article, we’ll look at how to fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript.

How to fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript?

To fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript, we should make sure our React component returns one root node or fragment.

For instance, we write:

import React from "react";

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

to return one div.

Or:

import React from "react";

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

to return a fragment.

Conclusion

To fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript, we should make sure our React component returns one root node or fragment.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *