Create React App
Create React App 是一个使用 Node.js 编写的命令行工具,通过它可以帮助我们快速生成 React.js 项目
1.安装
npm安装 : npm i -g create-react-app
yarn安装 : yarn global add create-react-app
2.创建项目
安装完成以后,即可使用 create-react-app 项目名称(英文小写)
命令
npx创建项目:npx create-react-app 项目名称
3.启动项目
创建完成后,在项目目录下,npm start
启动项目
启动一个内置的本地 WebServer,根目录映射到 ‘./public’ 目录,默认端口:3000
4.组件
对具有一定独立功能的数据和方法的封装,对外暴露接口,有利于代码功能的复用
组件分为函数组件和类式组件
5.事件
命名时,事件名首字母大写
不能通过返回false来阻止默认行为,必须使用preventDefault
事件中的this问题
class App extends Component{
//类式组件中,定义constructor,一定要接受props,调用super()
constructor(props){
super(props)
this.state = {nub:0,}
}
Fn(){
console.log(this) //this输出为undefined
}
fn=()=>{
console.log(this) //箭头函数才能找到this
}
render(){
let {nub} = this.state;
return (
<div>
<p>{nub}</p>
<button onClick={this.fn}>+1</button>
</div>
)
}
}