Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I was trying to connect my react app to a Mongodb database but this happened.

//index.js
ReactDOM.render(
  <React.StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </React.StrictMode>,
  document.getElementById("root")
);
like image 640
Robera Negussie Avatar asked Oct 12 '25 11:10

Robera Negussie


2 Answers

In React 18, is needed to:

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

creates your root container with this function:

const root = createRoot(document.getElementById("root"));

and then render your root app:

root.render(<YourApp />);

you can read this in: https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html

like image 144
Gabriel Alcântara Avatar answered Oct 15 '25 03:10

Gabriel Alcântara


Just replace your code with below in index.js file:

import ReactDOM from "react-dom/client";

const root = ReactDOM.createRoot(document.getElementById("root"))
root.render(<App />);
like image 39
Abhishek Jadav Avatar answered Oct 15 '25 02:10

Abhishek Jadav