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
Ganesh
Ganesh5mo ago
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
Faker
FakerOP5mo ago
it's mandatory to have one?
Jochem
Jochem5mo ago
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
Faker
FakerOP5mo ago
yep, it's like "syncing" the web page DOM with the virtual DOM of the framework? kind of? :c
Jochem
Jochem5mo ago
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
Faker
FakerOP5mo ago
yeah I see
Jochem
Jochem5mo ago
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
Faker
FakerOP5mo ago
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
Jochem
Jochem5mo ago
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 element
Faker
FakerOP5mo ago
yeah I see

Did you find this page helpful?