美文网首页
React的生命周期

React的生命周期

作者: sweetBoy_9126 | 来源:发表于2019-10-11 17:04 被阅读0次

使用js实现四个常用的生命周期

let app = document.getElementById("app");

// create div
let div = document.createElement("div");
div.style.border = "1px solid red";

let state = 0;
// render/update
div.innerHTML = `
  <p>${state}</p>
  <button>1</button>
  <button>die</button>
`;

// mount div
app.appendChild(div);
div.querySelector("button").onclick = () => {
  state += 1;
  // update/render div
  div.querySelector("p").innerText = state;
};

div.querySelectorAll("button")[1].onclick = () => {
  div.querySelector("button").onclick = null;
  div.querySelectorAll("button").onclick = null;
  div.remove();
  div = null; // destroy div
};

react

class Baba extends React.Component {
  constructor() {
    super();
    this.state = {
      hasChild: true
    };
  }
  killSon() {
    this.setState({
      hasChild: false
    });
  }
  talkSon() {
    this.setState({
      word: "爸爸说的话"
    });
  }
  render() {
    return (
      <div>
        <button onClick={() => this.killSon()}>kill son</button>
        <button onClick={() => this.talkSon()}>跟儿子说句话</button>
      // 在父组件中将子组件销毁
        {this.state.hasChild ? <App word={this.state.word} /> : null}
      </div>
    );
  }
}
class App extends React.Component {
  constructor() {
    super();
    this.state = {
      n: 0
    };
    console.log("创建 App");
  }
  onClick() {
    this.setState({
      n: this.state.n + 1
    });
  }
  componentWillMount() {
    console.log("将要挂载 App");
  }
  render() {
    // update
    console.log("填充/更新 App的内容");
    return (
      <div className="App">
        爸爸:
        {this.props.word}
        <br />
        {this.state.n}
        <button onClick={() => this.onClick()}>+1</button>
      </div>
    );
  }
  componentDidMount() {
    console.log("mount App 完毕");
  }
  componentWillUpdate() {
    console.log("update App 之前");
  }
  componentDidUpdate() {
    console.log("update App 之后");
  }
// 销毁组件,必须在父组件中将其销毁
  componentWillUnmount() {
    console.log("儿子被杀死前");
  }
  // 如果有父组件传来的数据就会触发当前生命周期
  componentWillReceiveProps() {
    console.log("爸爸说话了");
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<Baba />, rootElement);

案例运行地址:https://codesandbox.io/s/laughing-fire-pbc5m

react可以做的事情

  1. 请求数据(ajax)
    推荐在componentDidMount
    原因:为了性能的需要,Fiber有了调度render执行顺序的能力,所以componentWillMount执行变得不确定;
    constructor中你不能使用setState

  2. 更新数据setState
    1). 不能在mount之前setState
    2). 不能在rendersetState
    3). 不能再将要更新和更新完成的钩子里setState
    推荐在onclickcomponentDidMountcomponentWillReceivePropssetState

  3. 手动判断是否更新(shouldComponentUpdate)
    允许我们手动的判断是否进行组件更新,避免不必要的更新(如果不想更新就return false
    实例:
    state里添加一个x,我们在页面中不显示x,但是有一个按钮可以修改x,每一次点击按钮x后面会多加一个感叹号,正常情况下我们更新x会触发componentWillUpdaterendercomponentDidUpdate,如果我们想阻止更新的话只需要在shouldComponentUpdate里判断就可以了

this.state = {
      n: 0,
      x: "不展示"
    };
updateX() {
    this.setState({
      x: this.state.x + "!"
    });
  }
render() {
    // update
    console.log("填充/更新 App的内容");
    return (
      <div className="App">
        爸爸:
        {this.props.word}
        <br />
        {this.state.n}
        <button onClick={() => this.onClick()}>+1</button>
        <button onClick={() => this.updateX()}>updateX</button>
      </div>
    );
  }
  shouldComponentUpdate(nextProps, nextState) {
    if (this.state.n === nextState.n) {
      return false;
    } else {
      return true;
    }
  }

在用户点击一个按钮调用setState的时候会调用哪几个钩子?
shouldComponentUpdatecomponentWillUpdaterendercomponentDidUpdate

相关文章

  • React基础篇之组件的生命周期

    引出生命周期 react生命周期(旧) react生命周期(新) getSnapshotBeforeUpdate的...

  • React概念图

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

  • React生命周期

    React v16.0前的生命周期 React v16.4+ 的生命周期图 React v16.9后这些生命周期钩...

  • React v16 生命周期

    React 16 生命周期 React 16.3 新增的生命周期方法 逐渐废弃的生命周期方法: 一般将生命周期分成...

  • 学习并实现react (4)

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

  • react/vue常见问题整理

    一、react 1. react生命周期 react 16生命周期相对于15的变化:componentWillMo...

  • React面试题 整理脑图

    react基础 React生命周期 react-router react进阶 react Hooks redux 其他

  • 《深入React技术栈》学习笔记Ⅲ

    以下的生命周期都是在 React 15 的生命周期, React 16 的生命周期 API 已经发生变化。Reac...

  • React 组件生命周期

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

  • React总结

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

网友评论

      本文标题:React的生命周期

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