Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: Functions are not valid as a React child, using a conditional rendering


I'm trying to display the list of results taken from the response of wikipedia API. If the response doesn't show any list, it should show a different message. I am trying to do a conditional rendering.

Here is the code:

getData(e) {         e.preventDefault();         var search = e.target.search.value;         var wikipediaEndPoint = "https://en.wikipedia.org/w/api.php?format=json&origin=*&action=query&generator=search&gsrnamespace=0&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch="+search;         var wikipediaUrl = "https://en.wikipedia.org/?curid=";          fetch(wikipediaEndPoint)         .then(data => {             return data.json();         }).then(data => {             var objectList = (data.hasOwnProperty("query")) ? data.query.pages : "";             let resultList;              if(objectList !== ""){                 resultList = Object.keys(objectList).map(function(key, index) {                     return (                         <div className="item" key={objectList[key].pageid}>                             <a href={wikipediaUrl+objectList[key].pageid}>                                 <h1>{objectList[key].title}</h1>                                 <p>{objectList[key].extract}</p>                             </a>                         </div>                     )                 });             } else {                 resultList = (function() {                     return (                         <div className="item" key={1}>                             <p>No results!</p>                         </div>                     )                 });             }              this.setState({search: resultList});         })     } 

The result is shown here: <div className="items">{this.state.search}</div>

It works fine when the search field keyword finds some results, however, on the opposite case the console returns an error (or a warning):

Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.

Where is the error? Am I doing the conditional rendering the right way?

Thanks

like image 719
DamianFox Avatar asked Jan 21 '18 20:01

DamianFox


People also ask

How do you fix error objects are not valid as a React child?

js error "Objects are not valid as a React child" occurs when we try to directly render an object or an array in our JSX code. To solve the error, use the map() method to render arrays or access properties on the object in your JSX code.

Why objects are not valid as React child?

The "Objects are not valid as a React child" error happens when trying to render a collection of data by mistakenly returning the object when applying the map method instead of using the data from the object to create and return JSX.

What is conditional rendering in React?

In React, you can create distinct components that encapsulate behavior you need. Then, you can render only some of them, depending on the state of your application. Conditional rendering in React works the same way conditions work in JavaScript.

How do you render collection of children in React?

If you meant to render a collection of children, use an array instead' Error. We can fix the 'Objects are not valid as a React child. If you meant to render a collection of children, use an array instead' error by rendering an array with the map method or render objects as strings or render the properties individually.


1 Answers

            resultList = (function() {                 return (                     <div className="item" key={1}>                         <p>No results!</p>                     </div>                 )             }); 

Why are you setting a function? Just set the <div... -

            resultList = (                     <div className="item">                         <p>No results!</p>                     </div>                 ) 

Edit -

In order to really use a function (not sure why, though), the code would have to be a bit different -

            const Foo = (function() {                 return (                     <div className="item" key={1}>                         <p>No results!</p>                     </div>                 )             });             resultList = (<Foo />) 

Edit 2 -

The above edit works because React components do not have to be constructors/classes, they can also be a simple function that gets props as a parameter and returns JSX. I think this type of components is a bit limited, but it works.

like image 152
PhistucK Avatar answered Sep 19 '22 14:09

PhistucK