【发布时间】:2021-12-08 08:46:50
【问题描述】:
我有一个基于类的组件本身就是警告
函数组件不能被赋予 refs。尝试访问此 ref 将失败。你的意思是使用 React.forwardRef() 吗?
另外我无法参考参考
这是一个代码沙箱。 https://codesandbox.io/s/inspiring-currying-023wf?file=/src/App.js
【问题讨论】:
标签: reactjs reactstrap
我有一个基于类的组件本身就是警告
函数组件不能被赋予 refs。尝试访问此 ref 将失败。你的意思是使用 React.forwardRef() 吗?
另外我无法参考参考
这是一个代码沙箱。 https://codesandbox.io/s/inspiring-currying-023wf?file=/src/App.js
【问题讨论】:
标签: reactjs reactstrap
原来 reactstrap 中的Row 必须是一个功能组件。
因此而不是
<Row ref={this.myRef}>
...
</Row>
我不得不使用
<div ref={this.myRef}>
<Row>
...
</Row>
</div>
【讨论】:
react strap 中的 Row 组件是一个功能组件,因此不是实例,因此它不能具有 ref= 属性。
在此处查看文档。 https://reactjs.org/docs/refs-and-the-dom.html#refs-and-function-components。如果您想在该特定组件上使用 refs,那么这个链接也是 https://reactjs.org/docs/forwarding-refs.html。
如果您想扩大使用范围,也可以选择使用自定义类组件覆盖该组件。
【讨论】: