小编典典

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

reactjs

有什么方法可以检测孩子是否已被骑上?当我初始化同位素时,必须安装所有子组件以进行初始化。超时为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还是那正常吗?谢谢!


阅读 242

收藏
2020-07-22

共1个答案

小编典典

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

2020-07-22