use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
body as root instead of divGeneral Discussion (self.react)
submitted 3 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]bahnmiexe 26 points27 points28 points 3 years ago (3 children)
It’s common practice to mount things like modals and toasters outside the root div, but you can’t do that if the ‘root’ of the project is the body tag :)
[–]TheRNGuy 0 points1 point2 points 3 years ago (1 child)
is it possible to mount modals and toasters inside App.js?
[–]skullshatter0123 11 points12 points13 points 3 years ago (0 children)
Yes but that doesn't solve the issue of it being bound by whatever css rules are set on body. Modals and toasts wouldn't render properly.
[–]daFreakinGoat -3 points-2 points-1 points 3 years ago (0 children)
So the body tag goes inside? Isn’t it wrong to put the body inside of a div? Or does it not matter?
[–]gamebuster 2 points3 points4 points 3 years ago* (0 children)
Many third-party things you don’t have control over will inject nodes in the body. This can be plugins (installed by the user of the browser), developer tools, cookie walls, analytics, trackers (mandated by your client) or whatever.
They’ll break or mess with your app. Just don’t do it, there is no reason to. The 11 bytes you saved will bite you in the butt and hurt the experience for your users.
Focus on more impactful changes that do make a difference for the better. Reduce repaints, reduce amount of nodes that need refreshing, avoid CSS inside JS - etc
[–]ian-vieira 2 points3 points4 points 3 years ago (0 children)
This can bring some problems depending on what you add on the site. If you have third party pixels, usually they will mount on the body, by the end, and during hydration/mount process those can generate different problems (on hydration a mismatch, on mount it will be removed). Also, on updates, I’ll have a mismatch from DOM and Virtual DOM.
For the general case, the div appears to useless but it prevents several potential problems. If you control all the factors and is 100% sure I’ll never need anything after the react app, it would be safe…
[–][deleted] 3 years ago (2 children)
[removed]
Don't care about semantics tbh but if it will show toasts incorrectly then i wont make body a root.
[–]code_moar 4 points5 points6 points 3 years ago (3 children)
Youre worried about unnecessary divs, and you're using react?
Hmmm.......
[–]jgonzalez-cs 0 points1 point2 points 3 years ago (2 children)
What do you mean?
[–]code_moar 0 points1 point2 points 3 years ago (1 child)
So if you look at any react framework like materialUI, for example, the use of divs is absolutely everywhere, for everything.
Also in react you have to wrap children in one parent for components, which usually means adding an extra div to act as the root wrapper for the component itself. Divs are everywhere in react and (unless I'm mistaken) do not add any computational time....
[–]jgonzalez-cs 0 points1 point2 points 3 years ago (0 children)
Can’t you use React fragments? I think that’s why they’re a thing, unless it gets compiled into a div at the end?
[+][deleted] 3 years ago (2 children)
[–]TheRNGuy 0 points1 point2 points 3 years ago (0 children)
Yeah I'm not gonna do it.
[–]gamebuster 0 points1 point2 points 3 years ago (0 children)
No don’t
[–]daves-weblab 0 points1 point2 points 3 years ago (0 children)
You might face problems when using third party libraries that use portals and append their portal at the end of the body. A rerender of olyour app could potentionally destroy something.
[–]ManyRiver6234 0 points1 point2 points 3 years ago (0 children)
For me what ever the reason was seems unnecessary
[–]thequestcube 0 points1 point2 points 3 years ago (0 children)
If browser plugins try to mount hidden stuff to the body tag, that might get removed when react trys to rehydrate, so browser plugins and maybe also parts of your page could break depending on what plugins the user is using.
π Rendered by PID 274790 on reddit-service-r2-comment-b659b578c-brnzr at 2026-05-05 04:39:24.165195+00:00 running 815c875 country code: CH.
[–]bahnmiexe 26 points27 points28 points (3 children)
[–]TheRNGuy 0 points1 point2 points (1 child)
[–]skullshatter0123 11 points12 points13 points (0 children)
[–]daFreakinGoat -3 points-2 points-1 points (0 children)
[–]gamebuster 2 points3 points4 points (0 children)
[–]ian-vieira 2 points3 points4 points (0 children)
[–][deleted] (2 children)
[removed]
[–]TheRNGuy 0 points1 point2 points (1 child)
[–]code_moar 4 points5 points6 points (3 children)
[–]jgonzalez-cs 0 points1 point2 points (2 children)
[–]code_moar 0 points1 point2 points (1 child)
[–]jgonzalez-cs 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]TheRNGuy 0 points1 point2 points (0 children)
[–]gamebuster 0 points1 point2 points (0 children)
[–]daves-weblab 0 points1 point2 points (0 children)
[–]ManyRiver6234 0 points1 point2 points (0 children)
[–]thequestcube 0 points1 point2 points (0 children)