[英]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-app与custom-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')
);
node_modules
and run yarn install
node_modules
并运行yarn install
yarn start
yarn start
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 install
; npm 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
版本保留在最新版本,只更改了react
和react-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.json
和index.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 start
或yarn 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 步开始
First create react app npx create-react-app.首先创建react app npx create-react-app。 (. Means current file)
(。表示当前文件)
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 的要求版本以及其他要求
(it will download all data as per package.json) (它将根据 package.json 下载所有数据)
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.这个。
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
中,与以前的版本有些不同。
You go to your folder file structure and delete both package-lock.json
( yarn.lock
for yarn) file and node_modules
file.您转到文件夹文件结构并删除
package-lock.json
( yarn.lock
表示纱线)文件和node_modules
文件。
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
依赖文件中,像这样。
index.js
file change,index.js
文件更改后,import ReactDOM from 'react-dom/client'
to至
import ReactDOM from 'react-dom';
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')
);
terminal
and runterminal
并运行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..它对我有用..
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
For installing react-dom version 17.0.2 run this command.要安装 react-dom 17.0.2 版,请运行此命令。
npm install --save react-dom@17.0.2
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.