一.React脚手架使用
(1). 创建脚手架
首先:初始化项目
npx create-react-app my-app
(2).脚手架中使用React
首先: 导入 react react-dom两个包
import React from 'react'
import ReactDOM from 'react-dom'
然后:创建元素
let h1 = React.createElement('h1',null,'我是标题')
最后:渲染到页面
ReactDOM.render(h1,document.getElementById('root'))
二. npx介绍
不使用npx下, 相当于这样的命令
npm install -g create-react-app // 安装create-react-app全局包
create-react-app my-react-app // 使用 create-react-app 命令安装 my-react-app
使用了 npx以后 直接可以省略 npm install -g create-react-app 这一步
npx create-react-app my-app
描述:
1.npx是 npm5.2.0版本新增的一个工具包 , 定义为npm包的执行者 , 相比npm , npx会自动安装依赖包并执行某个命令
2.使用 create-react-app 脚手架创建 react项目 , 常规的做法先安装 create-react-app 然后才能使用 create-react-app 执行命令进行项目创建
3.npx会在当前目录下的 node_moduels/.bin 里面去查找是否有课执行命令 没有找到的话会去全局里面查找是否有对应的模块 全局也没有的话就自动下载对应的模块
create-react-app , npx会将 create-react-app 下载到一个临时目录 ,用完删除,不会占用本地资源