网站定制公司报价,如何用visual做网站,帝国cms做企业网站,wordpress+主题+恢复声明周期与组件卸载
props配置#xff1a;使用组件时传入数据state私有数据#xff1a;组件内部使用的数据 state的使用注意事项 必须使用setState方法来更改state多个setState会合并调用props和state更新数据要谨慎#xff08;有可能在异步程序中更新#xff09;setState…声明周期与组件卸载
props配置使用组件时传入数据state私有数据组件内部使用的数据 state的使用注意事项 必须使用setState方法来更改state多个setState会合并调用props和state更新数据要谨慎有可能在异步程序中更新setState操作合并的原理浅合并即设置什么属性就更新什么最终再合并成一个state // 不要这样去更新
this.setState({result: this.state.result this.props.content
})
// 使用这种方式
this.setState((state, props) {// state 上一个state// porps 此次更新时被使用的propsstate.result state.result props.content
})
// 设置arr用一个全新的数组替换而不再使用原先的引用
this.setState({arr: [...this.state.arr, 4]// or用数组的concat方法返回新数组
}) state是组件内部特有的数据封装其他组件无法读写该组件的state组价可以通过其他组件调用时传入的属性来传递state的值props虽然是响应式的但在组件内部是只读的state只能传递给自己的子组件state的安全影响范围组件可以没有状态组件有无状态可以切换原先无状态在生命周期函数/绑定的时间处理函数中增加状态总结这种数据状态从父到子由上而下传递的方式叫单向数据流 class MyTime extends React.Component {constructor(props) {super(props)}state {time: new Date().toString()}// 组件已经被渲染到了DOM中运行的函数componentDidMount() {this.t setInterval(() {this.setState({time: new Date().toString()})}, 1000)}// 组件即将被卸载时componentWillUnmount() {clearInterval(this.t)this.t nullconsole.log(over)}render() {return (divh1{this.state.time}/h1/div)}
}
ReactDOM.render(MyTime /,document.getElementById(app)
)
setTimeout(() {ReactDOM.unmountComponentAtNode(document.getElementById(app))
}, 3000)