简体   繁体   English

如何在较旧的 React 版本中使用 create-react-app?

[英]How to use create-react-app with an older React version?

When using create-react-app with custom-react-scripts I always end up with React 16 (latest) installed.当将create-react-appcustom-react-scripts一起使用时,我总是安装 React 16(最新)。 Is there a way to create a new project with an older version, like React 15?有没有办法用旧版本创建一个新项目,比如 React 15?

If you're here because of react v18, and you want to go down to the previous non-change breaking version, here's what i did:如果你是因为 react v18 而来到这里,并且你想回到以前的非更改破坏版本,这就是我所做的:

in your package.json replace:在你的package.json替换:

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

with

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

Then go to your entry file index.js At the top, replace:然后到你的入口文件index.js顶部,替换:

import ReactDOM from 'react-dom/client'

with

import ReactDOM from 'react-dom';

Still in your index.js file, replace:仍在您的index.js文件中,替换:

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

with

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);
  • Delete your node_modules and run yarn install删除你的node_modules并运行yarn install
  • After installation, run yarn start安装后运行yarn start
  • Enjoy享受

It seems that you can just go ahead and use it to generate a React 16 app, then edit package.json and run npm i (or npm install ; npm i is just a shortcut).似乎您可以继续使用它来生成一个 React 16 应用程序,然后编辑 package.json 并运行npm i (或npm installnpm i只是一个快捷方式)。

I just left the react-scripts version at the latest version and only changed the versions of react and react-dom , and it worked:我只是将react-scripts版本保留在最新版本,只更改了reactreact-dom的版本,它起作用了:

"devDependencies": {
  "react-scripts": "1.0.14"
},
"dependencies": {
  "react": "^15.6.1",
  "react-dom": "^15.6.1"
},

Complementing the answers described above, after all the manipulation, I had to update this libs(for TS template) :作为上述答案的补充,在所有操作之后,我不得不更新这个库(用于 TS 模板):

"@testing-library/react": "^12.1.1",
"@testing-library/user-event": "^12.5.0"

I ran into this problem, especially with the new create-react-app v18.我遇到了这个问题,尤其是使用新的 create-react-app v18。 All I did was to delete my package-lock.json file and also my node_modules folder.我所做的只是删除了我的package-lock.json文件以及我的node_modules文件夹。 I edited package.json afterwards, replaced react-dom and react with the version that I want as below:之后我编辑了package.json ,替换了 react-dom 并使用我想要的版本做出反应,如下所示:

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

Finally, I installed my packages again using npm install最后,我使用npm install再次安装了我的包

If you already created with React 18 , you can downgrade version 18 to previous verions of react with a few steps.如果您已经使用React 18创建,您可以通过几个步骤将版本 18 降级到以前的 react 版本。 You will need to do modifications in two files which are package.json and index.js .您需要在package.jsonindex.js两个文件中进行修改。 You should delete also node_modules您还应该删除node_modules

Step 1步骤1

Your index.js probably looks like this now您的index.js现在可能看起来像这样

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>
);

You should change import of ReactDOM and cancel const root line您应该更改ReactDOM的导入并取消const root

New code of index.js should be like this index.js的新代码应该是这样的

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

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

Step 2第2步

Go to your package.json file and update desired react version in dependencies转到您的package.json文件并在依赖项中更新所需的反应版本

"dependencies": {
    ...
    "react": "^17.0.0",
    "react-dom": "^17.0.0",
    ...
  },

Step 3第 3 步

Delede your node_modules and If you are using npm do npm i .删除你的 node_modules 并且如果你使用的是npm do npm i If you are using yarn do yarn install .如果您使用的是yarn ,请执行yarn install After this step you are good to run app with npm start or yarn start .在此步骤之后,您可以使用npm startyarn start运行应用程序。

Step 4 (OPTIONAL: If you are still having problems but I do not recommend doing it)第 4 步(可选:如果您仍有问题,但我不建议这样做)

Delete your package-lock.json (for npm ) or yarn.lock file (for yarn ) and start back from Step 3删除你的package-lock.json (对于npm )或yarn.lock文件(对于yarn )并从第 3 步开始

  1. First create react app npx create-react-app.首先创建react app npx create-react-app。 (. Means current file) (。表示当前文件)

  2. Delete node_modules & package-lock.json删除 node_modules & package-lock.json

 "@material-ui/core": "^4.12.2", "@material-ui/icons": "^4.11.2", "@testing-library/jest-dom": "^5.14.1", "@testing-library/react": "^11.2.7", "@testing-library/user-event": "^12.8.3", "firebase": "^8.8.0", "firebase-login": "^1.0.0", "firebase-tools": "^9.16.0", "react": "^17.0.2", "react-currency-format": "^1.0.0", "react-dom": "^17.0.2", "react-router-dom": "^5.2.0", "react-scripts": "4.0.3", "web-vitals": "^1.1.2"

As per require version of react & react-dom along with other required根据 react 和 react-dom 的要求版本以及其他要求

  1. Run command in bash/terminal npm install在 bash/terminal 中运行命令 npm install

(it will download all data as per package.json) (它将根据 package.json 下载所有数据)

  1. (optional) In project src/index.js (可选)在项目 src/index.js 中

 ReactDOM.render(`enter code here` <React.StrictMode> <App /> </React.StrictMode>, document.getE1ementById( 'root' ) ); Replace this with const root = ReactDOM.createRoot(document.getElementById('root')); root.render( <React.StrictMode> <App /> </React.StrictMode> );

This.这个。

  • When You Using当你使用
npx create-react-app app-name

it will always install latest react packages.它总是会安装最新的反应包。 Then, in react 18.2.0 , It is some of different about previous version.然后,在react 18.2.0中,与以前的版本有些不同。

  • If you want to downgrade your current react version to previous version of react, Try These 5 Steps .如果您想将当前的 React版本降级以前的 React 版本,请尝试这5 个步骤
  1. You go to your folder file structure and delete both package-lock.json ( yarn.lock for yarn) file and node_modules file.您转到文件夹文件结构并删除package-lock.jsonyarn.lock表示纱线)文件和node_modules文件。

  2. After you go to your package.json file and change and edit these dependencies :转到package.json文件并更改和编辑这些依赖项后:

"react": "^18.2.0",
"react-dom": "^18.2.0",

to

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

in your package.json dependencies file like this.在你的package.json依赖文件中,像这样。

  1. After go to Your index.js file change,转到您的index.js文件更改后,
import ReactDOM from 'react-dom/client'

to

import ReactDOM from 'react-dom';
  1. After change your index.js file and change below part of index file.更改index.js文件并更改索引文件的以下部分后。
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

to

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);
  1. Finally, You can open the terminal and run最后,您可以打开terminal并运行

if you are using npm -->> npm install如果您使用的是 npm -->> npm install

and after the installing part you can run app npm start .在安装部分之后,您可以运行 app npm start

or或者

if you are using yarn -->> yarn install如果您使用的是纱线 -->> yarn install

and after the installing part you can run app yarn start .在安装部分之后,您可以运行 app yarn start

--Thank You-- --Happy Coding-- --谢谢---编码愉快--

First you have to install npm in your system Then For Create react app you have to run below command首先你必须在你的系统中安装 npm 然后对于创建反应应用程序,你必须在命令下面运行

sudo npm init react-app my-app --scripts-version 1.1.5须藤 npm init react-app my-app --scripts-version 1.1.5

cd my-app cd 我的应用程序

sudo npm install --save react@16.0.0须藤 npm install --save react@16.0.0

sudo npm install --save react-dom@16.0.0须藤 npm install --save react-dom@16.0.0

it's working charm for me这对我来说很有魅力

It's WORKED for me..它对我有用..

  1. Just uninstall the react 18 and install the react 17.只需卸载 react 18 并安装 react 17 即可。

Run This Command运行此命令

npm uninstall react 

2.Then install React 17.0.2 Just run this command. 2.然后安装 React 17.0.2 只需运行此命令即可。

npm install react@17.0.2
  1. For installing react-dom version 17.0.2 run this command.要安装 react-dom 17.0.2 版,请运行此命令。

    npm install --save react-dom@17.0.2

  • Enjoy and Add Comment if it's worked for you如果它对您有用,请享受并添加评论

Specify your script version while create project在创建项目时指定您的脚本版本

sudo npm init react-app appname --scripts-version 1.0.13须藤 npm init react-app appname --scripts-version 1.0.13

its working for me它为我工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM