react组件生命周期
react组件生命周期??一个组件的生命周期无非四个阶段:初始化-挂载-更新-销毁,每个阶段react都定义了相应的钩子函数。 1. 创建阶段??创建阶段的工作主要是考虑如何去实例化一个组件对象,组件实例化阶段的所有操作都只会被执行一次,react组件实例化所包括的钩子函数如下: - getDefaultProps - getInitialState - componentWillMount - render - componentDidMount import React,{Component} from ‘react‘; import { Input,Select,Icon } from ‘antd‘; const Option = Select.Option; class App extends Component { constructor(props) { super(props); console.log(‘constructor is called‘); let {val} = this.props; console.log(val); } componentWillMount() { console.log(‘componentWillMount is called‘) } componentDidMount() { console.log(‘componentDidMount is called‘) } componentWillReceiveProps(nextProps,nextContext) { console.log(‘componentWillReceiveProps is called‘) } shouldComponentUpdate(nextProps,nextState,nextContext) { console.log(‘shouldComponentUpdate is called‘) } componentWillUpdate(nextProps,nextContext) { console.log(‘componentWillUpdate is called‘) } componentDidUpdate(prevProps,prevState,snapshot) { console.log(‘componentDidUpdate is called‘) } componentWillUnmount() { console.log(‘componentWillUnmount is called‘) } render() { console.log(‘render is called‘) return ( <div> </div> ); } } export default App; ??ok,在上面的一段程序中展现了React16中的主要的四个阶段的钩子函数: 初始化阶段?? 挂载阶段?? 更新阶段?? 销毁阶段?? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |