Avoiding Wrapper Divs in ReactJs
Hi everyone,
Just a quick post on how you can avoid wrapper divs in ReactJs. It’s particularly useful when populating tables or creating parts of a larger component. Instead of the following:
return (
<div>
<td>Frogs</td>
<td>Turtles</td>
</div>
);
You can use fragments:
import React, { Fragment } from 'react'
...
return (
<td>Frogs</td>
<td>Turtles</td>
);
The fragment won’t polute the dom and mean that you don’t need to add any additional styling. Check out these links for more info:
https://reactjs.org/docs/fragments.html
https://stackoverflow.com/a/49375945/522859
https://reactjs.org/blog/2017/11/28/react-v16.2.0-fragment-support.html