美文网首页
React组件的固有方法

React组件的固有方法

作者: Lonely小宇 | 来源:发表于2018-11-27 09:46 被阅读0次

定义一个React组件时,组件本身就带有一些特殊的属性和方法,这是一个完整的React的组件,他的这些方法名不应被自定义方法或属性占用,且这些方法本身就会在某些特定的情况下自动执行

示例

Class Example extends Component {
  constructor(props) {
    super(props);//继承父级的this及props,如果constructor中没有用到props可不填参数默认继承只this,建议填写
    this.state = {  //初始数据
      number: 0,
      bool: true,
      data:  this.props
  }

  componentWillMount() {
    console.log('组件将要挂载时执行此函数');//多用于服务端渲染
  }

  componentDidMount() {
    console.log('组件挂载时执行此函数');//第一次渲染完成,dom已生成
  }

  componentWillUnmount() {
    console.log('组件将要卸载时执行此函数');
  }

  render() {
    return (
      <div>这是组件的html模板</div>
    );
  }
}

相关文章

网友评论

      本文标题:React组件的固有方法

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