0

I am trying to use react suspense but I am facing some issue regarding rendering after making changes in react index.js file and I already installed react suspense "npm install react@experimental react-dom@experimental"

My index.js file

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";

ReactDOM.createRoot(document.getElementById("root")).render(<App />);

Index.js file

Error

TypeError: react_dom__WEBPACK_IMPORTED_MODULE_1___default.a.createRoot is not a function

Error Image

Ahsan Shakeel
  • 11
  • 2
  • 4
  • Did you search for the error message? It seems like it is this issue here: https://github.com/facebook/react/issues/18866 – rfestag Jul 07 '20 at 11:08
  • I tried to search but not getting any thing which might help 1- npx create-react-app my-app --template typescript 2- cd my-app && yarn upgrade react@experimental react-dom@experimental refrence types in react-app-env.d.ts 3- /// /// /// What does this 3rd point means? – Ahsan Shakeel Jul 07 '20 at 11:18
  • None of that should be relevant. Look at the answer provided in the issue linked above. React renamed `createRoot` to `unstable_createRoot` back in May. So try using `ReactDOM.unable_createRoot` instead of `ReactDOM.createRoot` – rfestag Jul 07 '20 at 11:23
  • Wow it's working now I am reading official docs so in there official docs they mentioned the old syntax Btw thanks :) – Ahsan Shakeel Jul 07 '20 at 11:32

4 Answers4

3

To anyone updating an old React App, the React support doc on React 18 says to write ONLY:

npm install react react-dom

for the React 18 update. I had two React 16 apps I wanted to update.

To do so, I first updated to React 17:

npm install react@17.0.0 react-dom@17.0.0

ONLY AFTER this version is installed properly, you are able to install to 18, specifying the version in the installation command:

npm install react@18.0.0 react-dom@18.0.0

After that,

ReactDOM.createRoot(document.getElementById("root")).render(<App />);

works just fine. Follow the version update on your 'package,json' file.

Jakub Kurdziel
  • 3,216
  • 2
  • 12
  • 22
Itay Barr
  • 31
  • 4
1

In order to works is required to use ReactDOM.unstable_createRoot

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";

ReactDOM.unstable_createRoot(document.getElementById("root")).render(<App />);
Lucas Matos
  • 2,531
  • 2
  • 15
  • 17
0

change -> import ReactDOM from "react-dom";

to -> import ReactDOM from "react-dom/client";

Fedor
  • 17,146
  • 13
  • 40
  • 131
0

The function createRoot is from react-dom/client. You can try the named import like this:

import React from "react";
import { createRoot } from "react-dom/client";
import App from "./App";

const root = createRoot(document.getElementById("root"));
root.render(<App />);
Rolazar
  • 120
  • 1
  • 8