Is it okay if I insert React component directly in document body by id?
Or it is better to wrap it in DIV?
index.js
ReactDOM.render(
<Provider store={store()}>
<App/>
</Provider>,
document.querySelector("#root")
)
index.html
<body class="d-flex flex-column h-100" id="root">
<noscript>You need to enable JavaScript to run this app.</noscript>
</body>
I don't recommend to use body as root of the virtual DOM for 3 reasons:
body normally contains script tags, which might include your React logic. Rerendering body means that you need to take care of all your scripts too, and everytime body needs to be re-rendered, all your scripts need to be reload again, which is not efficient, not to mention side effect of reloading React code (which I don't have any experience to justify). The only way to avoid this is to put all your scripts into header, which is not recommended. body, e.g. adding classes "modal-open" if you use Bootstrap modal, there are other solutions like using React's hook or an event listener dedicated to updating body.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With