Warning: ReactDOM.render is no longer supported in React 18

Question

Hi,

I’m getting this error message in my console:

react-dom.development.js:86 Warning: ReactDOM.render is no longer supported in React 18. 
Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17.

This is my index.js file:

import React from "react";
import ReactDOM from "react-dom";
import App from './components/App';
ReactDOM.render(<App />, document.getElementById('root'));

How do I fix this?

Answer ( 1 )

    0
    2023-02-05T15:18:50+00:00

    Instead of this

    ReactDOM.render(<App />, document.getElementById('root'));

    Try this

    import { createRoot } from 'react-dom/client';
    const rootElement = document.getElementById('root');
    const root = createRoot(rootElement);
    root.render(<App />);

Leave an answer