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

Updated at 15-Nov-2022, By samar

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

Through many examples, we will learn how to resolve the "Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead".

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

The error "ReactDOM.render is no longer supported in React 18. Use createRoot instead" occurs because the ReactDOM.render method has been deprecated in react 18.

If you want to remove this error message from your react app you have to use react-dom/client library and after that you can render the content in your react app using the render method on root node without any error message.

  • Render content using react-dom/client in react app

    Move to index.js file under src directory and copy/paste code in it.

    src\index.js

    import React from 'react';
    import * as ReactDOMClient from 'react-dom/client';
    
    
    const root = ReactDOMClient.createRoot(document.getElementById('root'));
    root.render("Hello world");
    

    Your index.html file code will look like below.

    public\index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>React App</title>
    </head>
    <body>
        <noscript> You need to enable JavaScript to run this app.</noscript>
        <div id="root"></div>
    
    </body>
    </html>
    

Back to code snippet queries related react