react入门(组件的生命周期)

引出生命周期

class Demo extends React.Component {
        state = {
          opacity: 0.1,
        };
        death = () => {
          //卸载组件
          ReactDOM.unmountComponentAtNode(document.getElementById("text"));
        };
        //组件挂载完毕
        componentDidMount() {
          console.log(1);
          this.timer = setInterval(() => {
            let { opacity } = this.state;
            opacity -= 0.1;
            console.log(opacity);
            if (opacity <= 0) {
              opacity = 1;
            }
            this.setState({
              opacity: opacity,
              // opacity,
            });
          }, 200);
        }
        //组件将要卸载时
        componentWillUnmount() {
          clearInterval(this.timer);
        }
        render() {
          return (
            <div>
              <div style={{ opacity: this.state.opacity }}>React学不会</div>
              <button onClick={this.death}>不活了</button>
            </div>
          );
        }
      }
      ReactDOM.render(<Demo />, document.getElementById("text"));
  //父组件
      class A extends React.Component {
        state = {
          carname: "奔驰",
        };
        changeCar = () => {
          this.setState({
            carname: "奥迪",
          });
        };
        render() {
          return (
            <div>
              <h2>我是父组件</h2>
              <B carName={this.state.carname}></B>
              <button onClick={this.changeCar}>换车名</button>
            </div>
          );
        }
      }
      //子组件
      class B extends React.Component {
        render() {
          return (
            <div>
              <h2>我是子组件</h2>
              <div>父组件传过来的车名是:{this.props.carName}</div>
            </div>
          );
        }
      }
      ReactDOM.render(<A />, document.getElementById("text"));

生命周期流程图(旧)

 生命周期的3个阶段

1初始化阶段:由ReactDOM.render()触发----初次渲染

  • constructor()
  • componentWillMount()组件将要挂载的钩子
  • render()
  • componentDidMount()组件挂载完毕的钩子    ===>常用    一般在这做初始化的事:开启定时器,发起网络请求,订阅消息  

2更新阶段:由组件内部this.setState()或父组件重新渲染render触发

  • shouldComponentUpdate()控制组件更新的阀门
  • componentWillUpdate()组件将要更新的钩子
  • render()  ===>必须使用的一个
  • componentDidUpdate() 组件更新完毕的钩子

3卸载阶段:由ReactDOM.unmountComponentAtNode()触发

  • componentWillUnmount() 组件将要卸载的钩子  ===>常用   一般在这做收尾的事:关闭定时器,取消订阅消息  

生命周期新

生命周期三个阶段(新)

1初始化阶段:由ReactDOM.render()触发----初次渲染

  • constructor()
  • getDerivedStateFromProps
  • render()
  • componentDidMount() 

2更新阶段:由组件内部this.setState()或父组件重新渲染render触发

  • getDerivedStateFromProps
  • shouldComponentUpdate()
  • render() 
  • getSnapshotBeforeUpdate
  • componentDidUpdate() 

3卸载阶段:由ReactDOM.unmountComponentAtNode()触发

  • componentWillUnmount() 

重要的钩子

  1. render:初始化渲染或者更新渲染调用
  2. componentDidMount:开启监听,发送ajax请求
  3. componentWillUnmount:做一些收尾工作,如:清除定时器

即将废弃的钩子

  1. componentWillMount
  2. componentWillReceiveProps
  3. componentWillUpdate

 新版本使用会出现警告,加上UNSAFE_前缀旧能使用了,一会可能会被彻底废弃,不建议使用

getSnapshotBeforeUpdate案例

class Demo extends React.Component {
        state = {
          newArr: [],
        };
        componentDidMount() {
          setInterval(() => {
            //获取原状态
            let { newArr } = this.state;
            //模拟一条新闻
            let news = "新闻" + (newArr.length + 1);
            //更新状态
            this.setState({
              newArr: [news, ...newArr],
            });
          }, 1000);
        }
        getSnapshotBeforeUpdate() {
          return this.refs.box.scrollHeight;
        }
        componentDidUpdate(preProps, preState, height) {
          //preProps preState height 都是之前的信息
          this.refs.box.scrollTop += this.refs.box.scrollHeight - height;
        }
        render() {
          return (
            <div className="box" ref="box">
              {this.state.newArr.map((item, index) => {
                return (
                  <div key={index} className="list">
                    {item}
                  </div>
                );
              })}
            </div>
          );
        }
      }

      ReactDOM.render(<Demo />, document.getElementById("text"));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值