Install React 18 and React DOM from npm or yarn, like this:
npm install react react-dom
Then, you’ll want to use createRoot
instead of render
.
In your index.js, update ReactDOM.render
to ReactDOM.createRoot
to create a root, and render your app using root.
Here’s what it would look like in React 17:
import ReactDOM from 'react-dom';
import App from 'App';
const container = document.getElementById('app');
ReactDOM.render(<App />, container);
And here’s what it looks like in React 18:
import ReactDOM from 'react-dom';
import App from 'App';
const container = document.getElementById('app');
// create a root
const root = ReactDOM.createRoot(container);
//render app to root
root.render(<App />);
That’s how easy it is to upgrade React to V18.