zoukankan      html  css  js  c++  java
  • 组件生命周期

    组件生命周期 
    React Component通过其定义的几个函数来控制组件在生命周期的各个阶段的动作。
    constructor(props, context) 
    构造函数,在创建组件的时候调用一次。
    
    void componentWillMount() 
    在组件挂载之前调用一次。如果在这个函数里面调用setState,本次的render函数可以看到更新后的state,并且只渲染一次。
    
    void componentDidMount() 
    在组件挂载之后调用一次。这个时候,子组件也都挂载好了,可以在这里使用refs。
    
    void componentWillReceiveProps(nextProps) 
    props是父组件传递给子组件的。父组件发生render的时候子组件就会调用componentWillReceiveProps(不管props有没有更新,也不管父子组件之间有没有数据交换)。
    
    bool shouldComponentUpdate(nextProps, nextState) 
    组件挂载之后,每次调用setState后都会调shouldComponentUpdate判断是否需要重新渲染组件。默认返回true,需要重新render。在比较复杂的应用里,有一些数据的改变并不影响界面展示,可以在这里做判断,优化渲染效率。
    
    void componentWillUpdate(nextProps, nextState) 
    shouldComponentUpdate返回true或者调用this.forceUpdate()之后,componentWillUpdate会被调用。
    
    void componentDidUpdate() 
    除了首次render之后调用componentDidMount,其它render结束之后都是调用componentDidUpdate。
    
    componentWillMount、componentDidMount和componentWillUpdate、componentDidUpdate可以对应起来。区别在于,前者只有在挂载的时候会被调用;而后者在以后的每次更新渲染之后都会被调用。
    
    ReactElement render() 
    render是一个React组件所必不可少的核心函数(上面的其它函数都不是必须的)。记住,不要在render里面修改state。
    
    void componentWillUnmount() 
    组件被卸载的时候调用。一般在componentDidMount里面注册的事件需要在这里删除。
    

      

  • 相关阅读:
    jieba分词
    hue审计记录-记录用户的查询记录(用户前端删除,后端也不会删除)
    nginx1.16.1平滑升级到1.18
    mysql5.7.24升级到5.7.30 rpm部署模式 redhat7
    ldap无法启动 system library:fopen:Permission denied bss_file.c:402
    hive练习-行列转换 窗口函数
    linkis重编译适配cdh
    redhat7 安装mysql5.15
    hive 自动加载分区 --动态分区
    最近搞了个客户端
  • 原文地址:https://www.cnblogs.com/gjack/p/9214516.html
Copyright © 2011-2022 走看看