【问题标题】:Adding ref to Reactstrap Jumbotron causes an error将 ref 添加到 Reactstrap Jumbotron 会导致错误
【发布时间】:2018-11-02 08:54:53
【问题描述】:

我正在尝试读取 Jumbotron 的高度。

为了做到这一点,我关注了this的回答。

唯一的区别是我将 ref 应用于 Reactstrap Jumbotron。 所以我添加了这段代码:

  <Jumbotron
    className="test"
    ref={ (divElement) => this.divElement = divElement}
  >

结果是这样的:

警告:无状态函数组件不能被赋予 refs。尝试 访问此 ref 将失败。

我应该如何克服这个问题?

【问题讨论】:

    标签: reactstrap


    【解决方案1】:

    我刚刚遇到了同样的问题,问题是 ReactStrap 使用功能组件来呈现标签,因此您不能在它们上使用 refs。只需将您的 jumbotron 转换为 div 而不使用 react strap 即可。

    &lt;div className="jumbotron" ref={this.divElement}&gt;&lt;/div&gt;

    【讨论】:

      猜你喜欢
      • 2016-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多