How to use create-react-app with an older React version

In this article, I would like to share with you that how to use create-react-app with an older React version” in your project file.

How to use create-react-app with an older React version

To use create-react-app with an older React version, you have to specify the version of package you want to use in your package.json.

If you have generated code using React 18 version and wish to downgrade to a prior version of react, you have to follow few steps. You will need to make changes to two files: package.json and index.js. You also have to remove the node modules.

There are multiple ways to a “create-react-app” with an older React version in your project file. You can follow one of the below-mentioned methods to create-react-app with an older React version depending on the react version you are currently using:

In case you are using the the latest version of react, that is react v18 or any other version of React; and if you wish to go down to a previous non-change breaking version, you have to follow the steps mentioned below:

In the package.json of your project file you will have to replace the following below mentioned code (Here is an example of using react version 18):

"react": "^18.0.0"
"react-dom": "^18.0.0"

replace the above mentioned code, with the version of react and react-dom you want to use in your project file:

"react": "^17.0.2"
"react-dom": "^17.0.2"

The next step is to go to the entry file index.js in your project file. At the top of the code, you have to replace the below mentioned code:

import ReactDOM from 'react-dom/client'

replace the above mentioned code, with the below mentioned code.

import ReactDOM from 'react-dom';

You will have to replace one more section in your index.js file, you have to replace the below mentioned code:

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

with the below mentioned code:

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

Please make sure to delete your node_modules and run the following command now “yarn install“.

After your yarn installation is finished using above command, then you have to run the following command “yarn start“.

This should help you understand how to use “create-react-app” with an older React version of have generated the code using latest React version 18.

Conclusion

To use “create-react-app” with an older React version, you have to specify the version of package you want to use in your package.json.

Hope this article has helped you to clarify all possible methods that how to use create-react-app with an older React version.