I am experimenting with React.
In the following script I have imported ‘createRoot’.
The browser console throws a warning:
You are importing createRoot from "react-dom" which is not supported.
You should instead import it from "react-dom/client".
However, in my script I have imported createRoot from ‘react-dom/client’.
What is going wrong here?
This is the current state of the script:
import React from 'react' ;
import * as ReactDOM from 'react-dom';
import {createRoot} from 'react-dom/client';
export default function TodoList() {
return(
<>
<h1>Lets do stuff!</h1>
</>
)
}
const app = document.getElementById('app');
const root = ReactDOM.createRoot(app);
2
Answers
You should use the
createRoot
, not fromReactDOM
PS: See React Doc
In React 18, you no longer import from
react-dom
directly. Instead, you import thecreateRoot
function fromreact-dom/client
and you create a root based on thediv
element withid="app"
you get from the DOM. Then, you render your<App />
component (or however you named it) by calling theroot.render
function and passing your component (which is the root of your app).Your
index.jsx
(orindex.js
file) should look like this: