I am using react with the following packages:
{
"name": "demo",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.0.1",
"@testing-library/user-event": "^13.5.0",
"h3-js": "^3.7.2",
"leaflet": "^1.7.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-leaflet": "3.0.2",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
My index.js
looks like the following:
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
reportWebVitals();
My App.js
is like the following:
import React from "react";
import { render } from "react-dom";
import LeafletMap from "./Map";
class App extends React.Component {
state = { resolution: 8, kRing: 0 };
constructor(props) {
super(props);
this.state = { resolution: 8, kRing: 0 };
}
render() {
return (
<div>
<LeafletMap
resolution={this.state.resolution}
kRing={this.state.kRing}
/>
Resolution:
<input
type="number"
min={0}
max={15}
onChange={this.onChangeResolution}
defaultValue={8}
/>
<br />
K Rings:
<input
type="number"
min={0}
max={100}
onChange={this.onChangeKRings}
defaultValue={0}
/>
</div>
);
}
onChangeResolution = (e) => {
this.setState({ resolution: Number.parseInt(e.target.value) });
};
onChangeKRings = (e) => {
this.setState({ kRing: Number.parseInt(e.target.value) });
};
}
export default App;
When I run my app with npm run start
I get the following error:
Compiled with problems:X
ERROR in ./src/index.js 5:0-40
Module not found: Error: Can't resolve 'react-dom/client' in '/home/Desktop/Code/demo_app/src'
I reinstalled all packages and its also listed in npm list
:
> npm list
demo_app@0.1.0 /home/Desktop/Code/demo_app
├── @testing-library/jest-dom@5.16.4
├── @testing-library/react@13.0.1
├── @testing-library/user-event@13.5.0
├── h3-js@3.7.2
├── leaflet@1.7.1
├── react-dom@17.0.1
├── react-leaflet@3.0.2
├── react-scripts@5.0.1
├── react@17.0.1
└── web-vitals@2.1.4
Any suggestions why I have problems compiling my application?
The final solution that worked for me was simply to change the React 18 index.js
file to the following:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
reportWebVitals();
You still have the older versions of "react" and "react-dom" in your dependencies. To solve it:
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react": "^18.1.0",
"react-dom": "^18.1.0",
npm install
This should solve your issue and update your app to react18You might need to downgrade React and react-dom, here is index.js
:
import React from "react";
import { render } from "react-dom";
import "./index.css";
import App from "./App";
const root = document.getElementById("root");
render(<App />, root);
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
In summary, if you are using React 17 (My version was 17.0.2), you index.js will have to be like the following,
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
if You are using React 18, your index.js file should looks like the following
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
I was facing problem, when I downgraded my react 18 project to react 17 project. i used,
npm install react@17 react-dom@17
For typescript project,
npm install react@17 @types/react@17 react-dom@17 @types/react-dom@17
I had a similar problem and I solved it by adding
"noImplicitAny": false
on the tsconfig.json
For react version 18.1 (with typescript), this works for me
import React from "react";
import { createRoot } from "react-dom/client";
import reportWebVitals from "./reportWebVitals";
import App from "./App";
const root = createRoot(
document.getElementById("root") as HTMLElement
);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
reportWebVitals();
If you're from here from react-router 's tutorial on stackblitz.com, you can do the following:
package-lock.json
{
"name": "tutorial",
"private": true,
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"serve": "vite preview"
},
"dependencies": {
"history": "^5.2.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-router-dom": "6.0.0-beta.8"
},
"devDependencies": {
"@rollup/plugin-replace": "^2.2.1",
"@types/node": "14.x",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@vitejs/plugin-react": "^1.0.1",
"typescript": "^4.3.5",
"vite": "^2.5.0"
}
}
npm install
The problem is React version mismatch. React-router tutorial uses React v18+, while the template on stackblitz.com is configured to use React v17
Ok. Firstly this is what I did. I experienced this problem on React ^16.14.0. I removed the 'client' from 'import ReactDOM from "react-dom/client" in my index.js file. I also commented out the
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
that was there and used
ReactDOM.render(
<App />,
document.getElementById("root")
);
instead. Also, commented out
import reportWebVitals from "./reportWebVitals"; and reportWebVitals();
also. After doing this your code should compile successfully.**
Open your terminal in your project's root directory and run the following command:
npm install react-dom@latest react@latest
After installed React 18, if you are still facing an error, you will have to change your index.js file to the as mentioned below:
import React from 'react'; import ReactDOM from 'react-dom/client'; import './index.css'; import App from './App'; const root = ReactDOM.createRoot(document.getElementById('root')); root.render( <React.StrictMode> <App /> </React.StrictMode> );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.