With React 18, a lot of new features and APIs were introduced with breaking changes from the previous versions. I have covered the common bugs and errors that occurred while initializing a React project with version 18. The depreciation of the ReactDOM.render method,
DOM element error in createRoot() method ,
and so many more. Alright, I will now jump straight to what you came here for.
The Index.js
So how should a root index.js file on a React 18 project look like?
import React from "react";
import { createRoot } from "react-dom/client";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
const rootElement = document.getElementById("root");
const root = createRoot(rootElement);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
reportWebVitals();
The above code suffices everything needed for a base React 18 project index file.
If you are using TypeScript, then there is an additional step that needs to be done to avoid the "argument not assignable" error. Read the article for the root index.ts file in the TypeScript based React 18 project.