检查是否所有子组件都已安装

IT技术 reactjs react-jsx
2021-05-22 05:04:20

有没有办法检测孩子是否已安装?当我初始化同位素时,必须为初始化安装所有子组件。超时为 5 毫秒,它按预期工作,但我确定有更好的方法。

componentDidMount: function() {
    var container = this.refs.vinesOverview.getDOMNode();

    setTimeout(function() {
      var isotope = new Isotope(container, {
        itemSelector: ".vine_item",
        layoutMode: "masonry",
        resizable: true,
        gutter: 0,
        isFitWidth: true
      });

      this.setState({ isotope: isotope });
    }.bind(this), 5);
}

更新

我现在试过这个:

componentDidMount: function() {
    var container = this.refs.vinesOverview.getDOMNode();
    console.log(container.offsetHeight); // console output 0
    setTimeout(function() {
        console.log(container.offsetHeight); // console output 3150
    }, 5);
  },

那么在 5ms 之后它计算了高度?这就是同位素不起作用的原因。这是BUG还是正常?谢谢!

1个回答

React 在调用componentDidMount组件之前等待挂载所有子组件如果您发现不正确的重现案例,请提交错误。