1

我有一些问题......为什么我们有这样的 componentWillUnmount 序列?从父母到它的孩子。

有可能逆转并冒泡吗?从孩子到父母?

代码沙盒

import React from "react";
import ReactDOM from "react-dom";

class Man extends React.PureComponent {
  componentWillUnmount = () => {
    console.log("Man");
  };

  render = () => "Man";
}
class Hello extends React.Component {
  componentWillUnmount = () => {
    console.log("Hello");
  };

  render() {
    return <p>Hello {this.props.children}</p>;
  }
}
class Root extends React.PureComponent {
  state = {
    visible: true
  };

  render = () => (
    <React.Fragment>
      <button
        onClick={() => this.setState(({ visible }) => ({ visible: !visible }))}
      >
        Click
      </button>

      {this.state.visible && (
        <Hello>
          <Man />
        </Hello>
      )}
    </React.Fragment>
  );
}
ReactDOM.render(<Root />, document.getElementById("container"));

4

0 回答 0