React提供了一个用于创建React项目的脚手架create-react-app,使用脚手架开发项目的特点:模块化、组件化、工程化。
1、全局安装create-react-app:
npm i create-react-app -g
2、切换到想创项目的目录,使用命令:
create-react-app my-app
或使用以下命令直接创建一个名为my-app的项目:
npx create-react-app my-app
3、安装好了之后,接下来进入这个目录:
cd my-app
4、安装依赖(create-react-app my-app方式安装的不需要):
npm install
5、启动项目:
npm start
6、create-react-app慢的解决方法:
在用create-react-app创建新应用时,会一直卡住,
create-react-app react-demo
这是因为create-react-app指令默认调用npm,于是直接把npm的register给永久设置过来就好了,这样使用cnpm或者npm就没差别了。如下:
npm config set registry https://registry.npm.taobao.org
// 配置后可通过下面方式来验证是否成功
npm config get registry
// 或npm info expres
设置成功后,再执行创建指令。
7、安装ant-design:
npm i antd -S
8、安装路由:
npm i react-router react-router-dom -S
9、安装redux:
npm i redux -S
10、支持less:
参考另一篇博文:解决React脚手架create-react-app中不能使用less问题
https://blog.csdn.net/duansamve/article/details/108229479
注:用create-react-app脚手架安装后可以直接支持jsx文件,不需要再去下载babel相关文件及配置webpack。
11、工程目录说明:
public/index.html:
src/index.js:
![](https://img-blog.csdnimg.cn/20210505102812822.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2R1YW5zYW12ZQ==,size_16,color_FFFFFF,t_70)