React Error: Invalid React Child
This error occurs when the render method attempts to render an invalid React child, which can happen when returning an array or object instead of a valid React element.
In your case, the error is caused by referencing the bound method this.onItemClick.bind(this, item) directly within the map function. To fix it, you should use an arrow function instead:
<code class="javascript">render() { const items = ['EN', 'IT', 'FR', 'GR', 'RU'].map((item) => { return (<li onClick={(e) => this.onItemClick(e, item)} key={item}>{item}</li>); }); // ... }</code>
The arrow function creates a new scope where the this value is bound to the component instance, allowing you to access the onItemClick function.
Understanding the Error Message
The error message includes the following information:
Additional Notes
<code class="javascript">onClick={(e) => this.setState((prevState) => ({ lang: item }))}</code>
The above is the detailed content of Why am I getting the 'Invalid React Child' error when mapping an array in my `render` method?. For more information, please follow other related articles on the PHP Chinese website!