美文网首页
react之组件的生命周期

react之组件的生命周期

作者: 小猿_Luck_Boy | 来源:发表于2017-09-01 10:27 被阅读33次

生命周期的方法

void constructor(props)

组件的构造,我们可以再这里初始化数据,调用一次

void componentWillMount()

组件将要渲染,调用一次,在render调用之前

ReactElement render()

这个是组件最重要的,必须实现的且必须包含返回值,一般我们在这里返回我们的组件信息

void componentDidMount()

组件渲染完成 ,调用一次,在render调用之后

void componentDidUpdate(prpos,state)

组件更新完毕,接收prposstate新的参数,propsstate是新的传入对象,当组件更新完成调用,可多次调用
s
void componentWillReceiveProps(prpos)

父控件向子控件传值,组件将要收到新的值, 接收prpos新的参数,可多次调用

void componentWillUnmount()

组件卸载(webStorm会自动提示补全成componentWillUnMount,这是错误的),调用一次

bool shouldComponentUpdate(prpos,state)

组件是否重新绘制 接收prposstate新的参数,可多次调用

该方法是用来优化的react性能的,可以判断新的props和以前的props值是否发生改变来决定是否重新绘制组件

return true 重新绘制

return false 不重新绘制

例子

class CustomButton extends Component {
    constructor(props){
        super(props)
        this.state = {
            count:0
        }
    }
    componentWillMount() {
        console.log("componentWillMount")
    }
    componentDidMount() {
        console.log("componentDidMount")
    }
    componentDidUpdate(nextProps, nextState) {
        console.log("componentDidUpdate")
    }
    componentWillReceiveProps(props) {
        console.log("componentWillReceiveProps",props);
    }
    componentWillUnmount() {
        console.log("componentWillUnmount")
    }
    shouldComponentUpdate(nextProps, nextState) {
        console.log("shouldComponentUpdate",nextProps);
        return true;
    }
    onClick(){
        this.setState(
            {
                count:++this.state.count
            }
        )
        console.log("控件更新组件的props")
    }

    render(){
        return(
            <button onClick={()=>this.onClick()}>
                {this.state.count}
            </button>
        )
    }
}
class HelloState extends React.Component {
  constructor(props) {
      super(props);
      this.state = {
          loading:true,
          update:""
      };
  }
  click(){
      console.log("卸载组件")
    this.setState({loading:false})
  }
    update(){
      console.log("父控件更新组件的props")
        this.setState({update:"1"})
    }
  render (){
    return (
        <div>
            <button onClick={this.click.bind(this)}>卸载</button>
            <button onClick={this.update.bind(this)}>更新</button>
            {this.state.loading ?<CustomButton update={this.state.update}/>:null
            }
        </div>
    )
  }
}

运行结果

点击CustomButton,再点击更新,最后点击卸载的运行的日志如下

相关文章

  • React概念图

    React概念图 React组件生命周期概念图 参考文档:React入门教程 组件生命周期React:组件生命周期...

  • React 组件生命周期

    组件生命周期 参考阅读: component-lifecycle react组件生命周期过程说明 react 组件...

  • React总结

    [toc] 1.React组件生命周期 1.1 生命周期图 组件的生命周期的图如下: 具体可参考React 组件生...

  • 学习并实现react (4)

    实现生命周期 生命周期介绍 React 生命周期图 React 子组件在父组件下的生命周期流程 实现 compon...

  • React Native学习笔记之组件生命周期

    React Native学习笔记之组件生命周期 单一组件生命周期 首先看一下RN组件生命周期简略说明图 组件初始化...

  • React Native 组件的生命周期

    React Native学习之组件的生命周期函数 iOS开发或者Android开发的同学肯定对组件的生命周期不陌生...

  • react(最近搞了一套react项目的视频 全套 有需

    React 组件生命周期在本章节中我们将讨论 React 组件的生命周期。 组件的生命周期可分成三个状态: Mou...

  • Notes On React - Two

    React 的生命周期   React组件 的生命周期大致可分成四个状态:  - Mounting:装配-组件实例...

  • 006@React组件的生命周期.md

    006@React组件的生命周期.md React Native 是基于组件化开发。弄清楚组件的生命周期对于我们开...

  • React 生命周期

    React 生命周期 初始化周期 组件重新渲染生命周期 组件卸载生命周期

网友评论

      本文标题:react之组件的生命周期

      本文链接:https://www.haomeiwen.com/subject/jwxcjxtx.html