react组件的生命周期
2017-05-11 15:42
477 查看
组件的生命周期分成三个状态:
1. Mounting: 已插入真实DOM;
2. Updating: 正在被重新渲染;
3. Unmounting: 已移出真实DOM
React 为每个状态都提供了两种处理函数,will 函数在进入状态之前调用,did函数在进入状态之后调用,三种状态共计五种处理函数。
1) componentWillMount();
2) componentDidMount();
3) componentWillUpdate(object nextProps, object nextState);
4) componentDidUpdate(object prevProps, object prevState);
5) componentWillUnmount()
此外,React还提供两种特殊状态的处理函数
1) componentWillReceiveProps(object nextProps) : 已加载组件收到新参数时调用
2) shouldComponentUpdate(object nextProps, object nextState) : 组件判断是否重新渲染时调用
AJAX
1. Mounting: 已插入真实DOM;
2. Updating: 正在被重新渲染;
3. Unmounting: 已移出真实DOM
React 为每个状态都提供了两种处理函数,will 函数在进入状态之前调用,did函数在进入状态之后调用,三种状态共计五种处理函数。
1) componentWillMount();
2) componentDidMount();
3) componentWillUpdate(object nextProps, object nextState);
4) componentDidUpdate(object prevProps, object prevState);
5) componentWillUnmount()
此外,React还提供两种特殊状态的处理函数
1) componentWillReceiveProps(object nextProps) : 已加载组件收到新参数时调用
2) shouldComponentUpdate(object nextProps, object nextState) : 组件判断是否重新渲染时调用
AJAX
var UserGist = React.createClass({ getInitialState: function() { return { username: '', lastGistUrl: '' }; }, componentDidMount: function() { $.get(this.props.source, function(result) { var lastGist = result[0]; if (this.isMounted()) { this.setState({ username: lastGist.owner.login, lastGistUrl: lastGist.html_url }); } }.bind(this)); }, render: function() { return ( <div> {this.state.username}'s last gist is <a href={this.state.lastGistUrl}>here</a>. </div> ); } }); ReactDOM.render( <UserGist source="https://api.github.com/users/octocat/gists" />, document.body );
相关文章推荐
- React组件生命周期-正确执行运行阶段的函数
- react学习笔记 item7 --- 组件的生命周期
- React 深入系列4:组件的生命周期
- react组件生命周期过程
- 深入浅出React+Redux(二:React 组件的生命周期)
- React(props+state+组件生命周期)
- react学习(6)——关于组件生命周期的问题
- react.js 从零开始(二)组件的生命周期
- React系列(六):React组件基础与生命周期
- React 组件生命周期
- React Native 中组件的生命周期
- React 组件的生命周期总结
- React组件生命周期
- React组件生命周期及组件之间的通信
- React总结2:React组件生命周期
- React Native 中组件的生命周期
- React组件生命周期
- React组件生命周期过程说明
- 理解React组件的生命周期
- 第一章、react 组件的生命周期