Use <React.Fragment> over <div>s where possible
See original GitHub issueWe wrap a lot of things in <span>
or <div>
when we don’t need to, simply because JSX needs us to enclose multiple node in a wrapper. But React has a tool for that: https://reactjs.org/docs/fragments.html
We should totally use it. 👍
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
Why are Fragments in React 16 better than container divs?
It's a tiny bit faster and has less memory usage (no need to create an extra DOM node). This only has a real...
Read more >React Fragment VS Div - DEV Community
A div has the prototype chain HTMLDivElement - HTMLElement - Element - Node - EventTarget, whereas a document-fragment has DocumentFragment - ...
Read more >Fragments - React
Fragments let you group a list of children without adding extra nodes to the DOM. There is also a new short syntax for...
Read more >React Fragments - Uses, Examples, Practices - KnowledgeHut
Both React Fragments and Divs can be used as wrappers, but using Div adds an extra node to the DOM Tree. 5. Why...
Read more >Blog - Understanding React Fragments (With Examples)
React Fragment is a component exposed by React which serves as a parent component in JSX but doesn't add anything to the DOM....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It looks like this issue is not really “actionable” to me, because it is a nice thing, yet not something we can do. I don’t think one should spend a lot of time reviewing all our components and decide to use/not use
React.Fragment
. (The problem with such issues is that they become stale and no one can really work on them)I’d rather encourage reviewers to suggest the use of
React.Fragment
when it makes sense, which has been the case multiple times already. On this basis, I am closing the issue.Sadly it’s not as simple as a find/replace, but it’s not too far off!
This patch will require looking through the components and finding instances where we’re wrapping things in a tag simply because JSX requires it. It’s probably a fair amount of manual judgment calls, but a good start would be to look for empty
<div>
and<span>
tags with no attributes/props–chances are they’re there simply to wrap other JSX and can be converted. You’ll want to do it one-by-one though and make sure it doesn’t break anything, but fortunately most of our CSS selectors don’t rely on nesting/tag-type so you should be 👍Side note: we do have a “good first bug” tag here: https://github.com/mozilla/addons-frontend/issues?q=is%3Aopen+is%3Aissue+label%3A"contrib%3A+good+first+bug" if you’re looking for other issues. This issue isn’t in there but come to think of it is probably fine as a contributor bug, so feel free to work on it.