总结-旧生命周期
初始化阶段: 由ReactDOM.render()触发---初次渲染
- constructor()
- componentWillMount()
- render()
- componentDidMount() ===> 常用
一般在这个钩子中做一些初始化的事,例如:开启定时器,发送网络请求,订阅消息
更新阶段: 由组件内部this.setState()或父组件render触发
- shouldComponentUpdate()
- componentWillUpdate()
- render() ===> 常用
- componentDidUpdate()
卸载组件: ReactDOM.unmountComponentAtNode()触发
组件挂载流程
- componentWillUnmount() ===> 常用
一般在这个钩子中做一些收尾的事,例如:关闭定时器,取消订阅消息
| class Count extends React.Component { |
| |
| constructor(props) { |
| super(props); |
| console.log('count---constructor'); |
| this.state = { count: 0 } |
| } |
| |
| |
| |
| componentWillMount() { |
| console.log('count---componentWillMount'); |
| } |
| |
| |
| componentDidMount() { |
| console.log('count---componentDidMount') |
| } |
| |
| componentWillUnmount() { |
| console.log('count---componentWillUnmount'); |
| } |
| |
| |
| shouldComponentUpdate(nextProps, nextState) { |
| console.log('count---shouldComponentUpdate'); |
| return true; |
| } |
| |
| |
| componentWillUpdate(nextProps, nextState) { |
| console.log('count---componentWillUpdate') |
| } |
| |
| componentDidUpdate(prevProps, prevState) { |
| console.log('count---componentDidUpdate'); |
| } |
| |
| add = () => { |
| let { count } = this.state; |
| count += 1; |
| this.setState({ count: count }); |
| } |
| unload = () => { |
| root.unmount(document.getElementById('test')); |
| } |
| |
| |
| render() { |
| const { count } = this.state; |
| console.log('count---render'); |
| return ( |
| <div> |
| <h2>当前求和为: {count}</h2> |
| <button onClick={this.add}>点我加一</button> |
| <button onClick={this.unload}>卸载组件</button> |
| </div> |
| ) |
| } |
| } |
父组件render流程
| class A extends React.Component { |
| constructor(props) { |
| super(props); |
| this.state = { carName: '奔驰' }; |
| } |
| changeCar = () => { |
| this.setState({ carName: '宝马' }); |
| } |
| render() { |
| return ( |
| <div> |
| <h1>A</h1> |
| <button onClick={this.changeCar}>换车</button> |
| <B carName={this.state.carName} /> |
| </div> |
| ) |
| } |
| } |
| |
| class B extends React.Component { |
| |
| |
| |
| componentWillReceiveProps(props) { |
| console.log('count---componentWillReceiveProps', props) |
| } |
| render() { |
| return ( |
| <div> |
| <h1>B</h1> |
| <h2>现在的车: {this.props.carName}</h2> |
| </div> |
| ) |
| } |
| } |
新生命周期
| class Count extends React.Component { |
| |
| |
| constructor(props) { |
| super(props); |
| console.log('count---constructor'); |
| this.state = { count: 0 } |
| } |
| |
| |
| static getDerivedStateFromProps(props, state) { |
| console.log('getDerivedStateFromProps', props, state); |
| return null; |
| } |
| |
| |
| getSnapshotBeforeUpdate = (prevProps, prevState) => { |
| console.log('getSnapshotBeforeUpdate'); |
| return 'nihao'; |
| } |
| |
| |
| componentDidMount() { |
| console.log('count---componentDidMount') |
| } |
| |
| componentWillUnmount() { |
| console.log('count---componentWillUnmount'); |
| } |
| |
| |
| shouldComponentUpdate(nextProps, nextState) { |
| console.log('count---shouldComponentUpdate'); |
| return true; |
| } |
| |
| componentDidUpdate(prevProps, prevState, snapshotValue) { |
| console.log('count---componentDidUpdate', snapshotValue); |
| } |
| |
| add = () => { |
| let { count } = this.state; |
| count += 1; |
| this.setState({ count: count }); |
| } |
| unload = () => { |
| root.unmount(document.getElementById('test')); |
| } |
| |
| |
| render() { |
| const { count } = this.state; |
| console.log('count---render'); |
| return ( |
| <div> |
| <h2>当前求和为: {count}</h2> |
| <button onClick={this.add}>点我加一</button> |
| <button onClick={this.unload}>卸载组件</button> |
| </div> |
| ) |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具