[React] 类组件/class组件
类组件的基本结构
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 
 | class Welcome extends React.Component { constructor(props) {
 super(props);
 this.state = {
 n: 0
 };
 }
 
 render() {
 return (
 <h1>Hello, {this.props.name}</h1>
 )
 }
 }
 
 
 | 
props和state
props是外部属性,state是内部属性
props只读不写,state能读能写,但是不能直接修改state
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 
 | class App extends React.Component {render() {
 return (
 <div className="App">
 <App2 messageForApp2="Hello" />
 </div>
 );
 }
 }
 
 class App2 extends React.Component {
 constructor() {
 super();
 this.state = {
 n: 0
 };
 }
 render() {
 return (
 <div className="App2">
 {this.props.messageForApp2}
 {this.state.n}
 </div>
 );
 }
 }
 
 
 | 
state的读写
state需要在constructor()中初始化,并通过setState进行修改
注意:
- 
this.state.n += 1同样可以运行,但是背离了React的函数式编程思想,因此强烈不推荐在React中通过直接修改的方式改变数据
 
- 
setState是异步更新数据,因此下面代码中add()中log出的n是还未更新的值,因此推荐使用函数的方式更新,可以获得最新值,如add2()所示
 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 
 | class App extends React.Component {constructor() {
 super();
 this.state = {
 n: 0
 };
 }
 add() {
 this.setState({ n: this.state.n + 1 });
 console.log(this.state.n)
 }
 add2() {
 this.setState(state => {
 const n = state.n + 1
 return {n}
 }
 }
 render() {
 return (
 <div className="App">
 n: {this.state.n}
 <button onClick={() => this.add()}>+1</button>
 </div>
 );
 }
 }
 
 
 | 
复杂state
类组件中的setState会自动合并第一层属性,但不会合并第二层属性,因此常使用...操作符
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 
 | class App extends React.Component {constructor() {
 super();
 this.state = {
 n: 0,
 m: 0,
 user: {
 name: "A",
 age: 18
 }
 };
 }
 addN() {
 this.setState({ n: this.state.n + 1 });
 
 }
 addM() {
 this.setState({ m: this.state.m + 1 });
 
 }
 changeUser() {
 this.setState({
 
 user: {
 ...this.state.user
 name: "jack"
 }
 });
 }
 render() {
 return (
 <div className="App">
 n: {this.state.n}
 <button onClick={() => this.addN()}>n+1</button>
 m: {this.state.m}
 <button onClick={() => this.addM()}>m+1</button>
 <hr />
 <div>user.name: {this.state.user.name}</div>
 <div>user.age: {this.state.user.age}</div>
 <button onClick={() => this.changeUser()}>change user</button>
 </div>
 );
 }
 }
 
 | 
事件绑定
主要有fn1和fn2两种写法,目前主流是fn2写法
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | class App extends React.Component {constructor() {
 super();
 this.fn2 = () => {...}
 }
 fn1() {...}
 fn2 = () => {...}
 render() {
 return (
 <div className="App">
 <button onClick={() => this.fn1()}>fn1</button>
 <button onClick={this.fn2}>fn2</button>
 </div>
 );
 }
 }
 
 | 
生命周期
- 
constructor()
 要记得super()
 在这里初始化state 
- 
shouldComponentUpdate()
 函数return true 则更新,return false则会阻止更新,可以让我们手动判断是否要进行组件更新,我们可以根据应用场景灵活设置返回值,以避免不必要的更新 
- 
render()
 创建虚拟DOM 
- 
componentDidMount()
 组件已出现在页面 
- 
componentDidUpdate()
 组件已更新 
- 
componentWillUnmount()
 组件将死