StrictMode and creatRoot in React
Hello, can someone explain how the StrictMode and createRoot works in react please, why do we need to include them in our main entry point of
.jsx? What if we omit them?
From what I've read, StrictMode is there to detect some errors that can happen at runtime. What about creatRoot? Can't we omit if?10 Replies
https://react.dev/reference/react-dom/client/createRoot
Create root specifies a root/container element to render the react components in
If you were using vite it's usually a div with ID of root that's used as container to render react components in
it's mandatory to have one?
yes, frameworks generally need a root component to insert components into
you use it to tell the framework where you want the components to live in the DOM
yep, it's like "syncing" the web page DOM with the virtual DOM of the framework? kind of? :c
theoretically, a framework could just use the body tag, but that gives you way less flexibility and is just a default for a root object
it's the element that is used as the parent for the elements the framework generates
yeah I see
it doesn't necessarily have anything to do with the virtual dom, as even frameworks that don't use a virtual dom still use a root element
oh ok
didn't know that but yeah I understand the concept, the framework needs to know that within a particular component how things are organised I guess
not within a component, the root is well... the root. The tree of components is rendered to HTML code, and the first node in HTML code (or the
HTMLElements its generated) is then inserted into the root as a child elementyeah I see