ReactNative生命周期

ReactNative的生命周期以运行时的状态来区分,可分为三类:

Mounting

Updating

Unmounting

当刚创建组件时,所调用方法的顺序如下:

Paste_Image.png

当刷新组件时,所调用方法的顺序如下:

Paste_Image.png

图中无componentWillReceiveProps方法的调用,是因为刷新时并未改变props对象。注意,组件初次被渲染时componentWillReceiveRrops方法并不会被触发。

props与state

声明Prop Types和Default Props

ES6

class Greeting extends Component {
   // ...
}
Greeting.propTypes = { name: React.PropTypes.string};
Greeting.defaultProps = { name: 'Mary'};

ES5,getDefalutProps需要声明为函数

let Greeting = React.createClass({ 
  propTypes: { name: React.PropTypes.string }, 
  getDefaultProps: function() { 
    return { name: 'Mary' }; 
  }, 
  // ...});

初始化state

ES6

class Counter extends React.Component { 
  constructor(props) { 
    super(props); 
    this.state = {count: props.initialCount}; 
  } 
  // ...
}

ES5,声明一个名为getInitialState的函数

let Counter = React.createClass({ 
  getInitialState: function() { 
    return {
      count: this.props.initialCount}; 
    }, 
    // ...
  });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容