ReactDOM.render is no longer supported in React 18.

ReactDom.render is no longer supported in React latest update 18.0.0. UsecreateRoot instead untill you switch to the new API, your app will behave as it it’s running React 17.
If you will try to run the ReactDom.render in index.js

then you will get …


This content originally appeared on DEV Community and was authored by Abhishek jaiswal

ReactDom.render is no longer supported in React latest update 18.0.0. UsecreateRoot instead untill you switch to the new API, your app will behave as it it's running React 17.
If you will try to run the ReactDom.render in index.js
Image description

then you will get warning like this.
Image description
A better alternative of this warning is change your dependency version of react and react-dom to some older version like 17.0.2 or some other which is better for you and for your project in package.json file inside the your project
Image description

or else you can use different method as well.
To solve the error, create a root element and use the ReactDOMClient.render method instead.

Make sure you are changing the index.js file


import {StrictMode} from 'react';
import ReactDOM from 'react-dom';
import App from './App';

// ⛔️ 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.
ReactDOM.render( // 👈️ deprecated starting React 18
  <StrictMode>
    <App />
  </StrictMode>,
  document.getElementById('root'),
);

To solve the error, create a root element and use the ReactDOMClient.render method instead.

import {StrictMode} from 'react';
import {createRoot} from 'react-dom/client';

import App from './App';

// 👇️ IMPORTANT: use correct ID of your root element
// this is the ID of the div in your index.html file
const rootElement = document.getElementById('root');
const root = createRoot(rootElement);

// 👇️ if you use TypeScript, add non-null (!) assertion operator
// const root = createRoot(rootElement!);

root.render(
  <StrictMode>
    <App />
  </StrictMode>,
);

Let's see what's new in this React!!

React 18 introduces a new root API which provides better tools for managing roots. The new root API also enables the new concurrent renderer, which allows you to opt-into concurrent features.

// Before
import { render } from 'react-dom';
const container = document.getElementById('app');
render(<App tab="home" />, container);

// After
import { createRoot } from 'react-dom/client';
const container = document.getElementById('app');
const root = createRoot(container); // createRoot(container!) if you use TypeScript
root.render(<App tab="home" />);

for more information plase visit it's official website:
Reactjs


This content originally appeared on DEV Community and was authored by Abhishek jaiswal


Print Share Comment Cite Upload Translate Updates
APA

Abhishek jaiswal | Sciencx (2022-04-16T21:19:36+00:00) ReactDOM.render is no longer supported in React 18.. Retrieved from https://www.scien.cx/2022/04/16/reactdom-render-is-no-longer-supported-in-react-18/

MLA
" » ReactDOM.render is no longer supported in React 18.." Abhishek jaiswal | Sciencx - Saturday April 16, 2022, https://www.scien.cx/2022/04/16/reactdom-render-is-no-longer-supported-in-react-18/
HARVARD
Abhishek jaiswal | Sciencx Saturday April 16, 2022 » ReactDOM.render is no longer supported in React 18.., viewed ,<https://www.scien.cx/2022/04/16/reactdom-render-is-no-longer-supported-in-react-18/>
VANCOUVER
Abhishek jaiswal | Sciencx - » ReactDOM.render is no longer supported in React 18.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/16/reactdom-render-is-no-longer-supported-in-react-18/
CHICAGO
" » ReactDOM.render is no longer supported in React 18.." Abhishek jaiswal | Sciencx - Accessed . https://www.scien.cx/2022/04/16/reactdom-render-is-no-longer-supported-in-react-18/
IEEE
" » ReactDOM.render is no longer supported in React 18.." Abhishek jaiswal | Sciencx [Online]. Available: https://www.scien.cx/2022/04/16/reactdom-render-is-no-longer-supported-in-react-18/. [Accessed: ]
rf:citation
» ReactDOM.render is no longer supported in React 18. | Abhishek jaiswal | Sciencx | https://www.scien.cx/2022/04/16/reactdom-render-is-no-longer-supported-in-react-18/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.