react语法 参考
生命周期
生命周期的三个状态
- Mounting
已经插入真实的DOM
- Updating
正在被重新渲染
- Unmounting
已经移除真实DOM
react为每个状态提供两种函数 will/进入状态之前 did/进入状态之后
- componentWillMount()
- componentDidMount()
- componentWillUpdate(object nextProps, object nextState)
- componentDidUpdate(object prevProps, object prevState)
- componentWillUnmount()
特殊处理的钩子函数
componentWillReceiveProps(object nextProps):已加载组件收到新的参数时调用
shouldComponentUpdate(object nextProps, object nextState):组件判断是否重新渲染时调用
组件通信
父组件通信
`子组件通过props可以访问父组件的参数`
非父子组件通信
使用全局事件 Pub/Sub 模式,在 componentDidMount 里面订阅事件,在 componentWillUnmount 里面取消订阅,当收到事件触发的时候调用 setState 更新 UI。这种模式在复杂的系统里面可能会变得难以维护,对于比较复杂的应用,推荐使用类似 Flux 这种单项数据流架构。