Change is the only proof of life. —— Evelyn Waugh 《Brideshead Revisited》
所有在JSX中声明的函数,都会被委托在最顶层的document节点上,并用组件名和事件名来存储回调函数,
每次当某个组件触发事件时,在document上绑定的监听函数就会找到这个组件和他所有的父组件,
对每个组件创建对应的react合成事件,并批处理,从而根据事件名和组件名调用回调函数
关于setState
- 原理:在执行this.setState()时,react并没有急着立即更新state,而是把新的state存到一个队列中。对传进去的对象进行合并,然后在统一处理,触发重新渲染过程。
- setState()并不会立即改变this.state,而是创建一个即将处理的state。setState()并不一定是同步的。为了提升性能React会批量执行state()和DOM渲染
- setState()总是会触发一次组件重绘,除非在showComponentUpdate()中实现了一些条件条件渲染逻辑
- 把setState()看做是重新render的一次请求而不是立即更新组件的指令
Q:调用this.setState()后什么时候this.state才会更新?
A:即将要执行下一次的render函数时
组件的生命周期
为了更清楚明了的查看组件的生命周期,在每一个生命周期中添加改生命周期的名称
发现
初次调用时
- father was created
- father componentWillMount
- render father
- child was created
- child compoentWillMount
- render Child
- Child componentDidMount
- Father componentWillUnmount
调用父组件的setState后
- Father shouldCompoentUpdate
- Father compoentWillpdate
- Father render
- Child componentWillReceiveProps
- Child shouldComponentUpdate
- Child componentWillUpdate
- render Child
- Child ComponentDidUpdate
- Father CompoentDidUpdate
1 | class Child extends React.Component { |
