React Native -- The Life-Cycle of a Composite Component

前端之家收集整理的这篇文章主要介绍了React Native -- The Life-Cycle of a Composite Component前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. /**
  2. * ------------------ The Life-Cycle of a Composite Component ------------------
  3. *
  4. * - constructor: Initialization of state. The instance is now retained.
  5. * - componentWillMount
  6. * - render
  7. * - [children's constructors]
  8. * - [children's componentWillMount and render]
  9. * - [children's componentDidMount]
  10. * - componentDidMount
  11. *
  12. * Update Phases:
  13. * - componentWillReceiveProps (only called if parent updated)
  14. * - shouldComponentUpdate
  15. * - componentWillUpdate
  16. * - render
  17. * - [children's constructors or receive props phases]
  18. * - componentDidUpdate
  19. *
  20. * - componentWillUnmount
  21. * - [children's componentWillUnmount]
  22. * - [children destroyed]
  23. * - (destroyed): The instance is now blank,released by React and ready for GC.
  24. *
  25. * -----------------------------------------------------------------------------
  26. */

测试用例--不更新:

  1. var SampleApp = React.createClass({
  2. getInitialState: function() {
  3. console.log('getInitialState');
  4. return {
  5. teams: null,};
  6. },componentWillMount: function(){
  7. console.log('componentWillMount');
  8. },getDefaultProps: function() {
  9. console.log('getDefaultProps');
  10. },componentDidMount: function() {
  11. console.log('componentDidMount');
  12. },render: function() {
  13. console.log('render');
  14. return this.renderLoadingView();
  15. },componentWillUpdate:function(){
  16. console.log('componentWillUpdate');
  17. },componentWillUnmount:function(){
  18. console.log('componentWillUnmount');
  19. },renderLoadingView: function() {
  20. return (
  21. <View style={styles.container}>
  22. <Text>
  23. The Life-Cycle of a Composite Component
  24. </Text>
  25. </View>
  26. );
  27. },});
测试结果:

  1. 2015-10-09 16:52:34.591 [info][tid:com.facebook.React.JavaScript] 'getDefaultProps'
  2. 2015-10-09 16:52:34.612 [info][tid:com.facebook.React.JavaScript] 'Running application "SampleApp" with appParams: {"rootTag":1,"initialProps":{}}. __DEV__ === true,development-level warning are ON,performance optimizations are OFF'
  3. 2015-10-09 16:52:34.620 [info][tid:com.facebook.React.JavaScript] 'getInitialState'
  4. 2015-10-09 16:52:34.621 [info][tid:com.facebook.React.JavaScript] 'componentWillMount'
  5. 2015-10-09 16:52:34.621 [info][tid:com.facebook.React.JavaScript] 'render'
  6. 2015-10-09 16:52:34.625 [info][tid:com.facebook.React.JavaScript] 'componentDidMount'

测试用例--更新:

  1. var REQUEST_URL = 'http://platform.sina.com.cn/sports_all/client_api?app_key=3571367214&_sport_t_=football&_sport_s_=opta&_sport_a_=teamOrder&type=213&season=2015&format=json';
  2.  
  3. var SampleApp = React.createClass({
  4. getInitialState: function() {
  5. console.log('getInitialState');
  6. return {
  7. teams: null,componentDidMount: function() {
  8. console.log('componentDidMount');
  9. this.fetchData();
  10. },render: function() {
  11. console.log('render');
  12. if (!this.state.teams) {
  13. this.state.times += 1;
  14. return this.renderLoadingView();
  15. }
  16. this.state.times += 1;
  17. var team = this.state.teams[1];
  18. return this.renderTeam(team);
  19. },fetchData: function() {
  20. fetch(REQUEST_URL)
  21. .then((response) => response.json())
  22. .then((responseData) => {
  23. this.setState({
  24. teams: responseData.result.data,});
  25. })
  26. .done();
  27. },renderLoadingView: function() {
  28. return (
  29. <View style={styles.container}>
  30. <Text>
  31. Loading teams...
  32. </Text>
  33. </View>
  34. );
  35. },renderTeam: function(team) {
  36. return (
  37. <View style={styles.container}>
  38. <Image
  39. source={{uri: team.logo}}
  40. style={styles.thumbnail}>
  41. </Image>
  42. <View style={styles.rightContainer}>
  43. <Text style={styles.name}>{team.team_cn}</Text>
  44. <Text style={styles.rank}>{team.team_order},{this.state.times}</Text>
  45. </View>
  46. </View>
  47. );
  48. }
  49. });
  50.  
  51.  
  52.  
  53. var styles = StyleSheet.create({
  54. container: {
  55. flex: 1,flexDirection: 'row',justifyContent: 'center',alignItems: 'center',backgroundColor: '#F5FCFF',},thumbnail: {
  56. width: 53,height: 81,marginLeft: 10,rightContainer: {
  57. flex: 1,name: {
  58. fontSize: 20,marginBottom: 8,textAlign: 'center',rank: {
  59. textAlign: 'center',});

测试结果:

  1. 2015-10-09 16:54:50.082 [info][tid:com.facebook.React.JavaScript] 'getDefaultProps'
  2. 2015-10-09 16:54:50.102 [info][tid:com.facebook.React.JavaScript] 'Running application "SampleApp" with appParams: {"rootTag":1,performance optimizations are OFF'
  3. 2015-10-09 16:54:50.109 [info][tid:com.facebook.React.JavaScript] 'getInitialState'
  4. 2015-10-09 16:54:50.109 [info][tid:com.facebook.React.JavaScript] 'componentWillMount'
  5. 2015-10-09 16:54:50.110 [info][tid:com.facebook.React.JavaScript] 'render'
  6. 2015-10-09 16:54:50.113 [info][tid:com.facebook.React.JavaScript] 'componentDidMount'
  7. 2015-10-09 16:54:50.237 [info][tid:com.facebook.React.JavaScript] 'componentWillUpdate'
  8. 2015-10-09 16:54:50.237 [info][tid:com.facebook.React.JavaScript] 'render'

组件的生命周期

组件的生命周期主要由三个部分组成:

  • Mounting:组件正在被插入DOM中
  • Updating:如果DOM需要更新,组件正在被重新渲染
  • Unmounting:组件从DOM中移除

React提供了方法,让我们在组件状态更新的时候调用will标识状态开始之前,did表示状态完成后。例如componentWillMount就表示组件被插入DOM之前。

Mounting

  • getInitialState():初始化state
  • componentWillMount():组件被出入DOM前执行
  • componentDidMount():组件被插入DOM后执行

Updating

  • componentWillReceiveProps(object nextProps):组件获取到新的属性时执行,这个方法应该将this.props同nextProps进行比较,然后通过this.setState()切换状态
  • shouldComponentUpdate(object nextProps,object nextState):组件发生改变时执行,应该将this.props和nextProps、this.stats和nextState进行比较,返回true或false决定组件是否更新
  • componentWillUpdate(object nextProps,object nextState):组件更新前执行,不能在此处调用this.setState()。
  • componentDidUpdate(object prevProps,object prevState):组件更新后执行

Unmounting

  • componentWillUnmount():组件被移除前执行

Mounted Methods

  • findDOMNode()获取真实的DOM
  • forceUpdate():强制更新

猜你在找的React相关文章