Fix – react.children.only expected to receive a single react element child

If you get react.children.only expected to receive a single react element child error, here is how to fix this issue. The issue happened because the…

The post Fix – react.children.only expected to receive a single react element child appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Pandu Rijal Pasa

If you get react.children.only expected to receive a single react element child error, here is how to fix this issue.

The issue happened because the component only expects a single ReactElement to its children. Here is an example that could give you a similar error:

<MyComponent>
  <div>First Children</div>
  <div>Second Children</div>
</MyComponent>

To fix the issue, don’t put more than one element on your children. If you still need to keep those multiple elements as children without any wrapper element, you can use ReactFragment to replace the wrapper. Here is an example to do that:

// using ReactFragment
<MyComponent>
  <React.Fragment>
    <div>First Children</div>
    <div>Second Children</div>
  </React.Fragment>	
</MyComponent>

// using short syntax of ReactFragment
<MyComponent>
  <>
    <div>First Children</div>
    <div>Second Children</div>
  </>	
</MyComponent>

The post Fix – react.children.only expected to receive a single react element child appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Pandu Rijal Pasa


Print Share Comment Cite Upload Translate Updates
APA

Pandu Rijal Pasa | Sciencx (2021-02-21T12:04:06+00:00) Fix – react.children.only expected to receive a single react element child. Retrieved from https://www.scien.cx/2021/02/21/fix-react-children-only-expected-to-receive-a-single-react-element-child/

MLA
" » Fix – react.children.only expected to receive a single react element child." Pandu Rijal Pasa | Sciencx - Sunday February 21, 2021, https://www.scien.cx/2021/02/21/fix-react-children-only-expected-to-receive-a-single-react-element-child/
HARVARD
Pandu Rijal Pasa | Sciencx Sunday February 21, 2021 » Fix – react.children.only expected to receive a single react element child., viewed ,<https://www.scien.cx/2021/02/21/fix-react-children-only-expected-to-receive-a-single-react-element-child/>
VANCOUVER
Pandu Rijal Pasa | Sciencx - » Fix – react.children.only expected to receive a single react element child. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/21/fix-react-children-only-expected-to-receive-a-single-react-element-child/
CHICAGO
" » Fix – react.children.only expected to receive a single react element child." Pandu Rijal Pasa | Sciencx - Accessed . https://www.scien.cx/2021/02/21/fix-react-children-only-expected-to-receive-a-single-react-element-child/
IEEE
" » Fix – react.children.only expected to receive a single react element child." Pandu Rijal Pasa | Sciencx [Online]. Available: https://www.scien.cx/2021/02/21/fix-react-children-only-expected-to-receive-a-single-react-element-child/. [Accessed: ]
rf:citation
» Fix – react.children.only expected to receive a single react element child | Pandu Rijal Pasa | Sciencx | https://www.scien.cx/2021/02/21/fix-react-children-only-expected-to-receive-a-single-react-element-child/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.