【发布时间】:2019-10-22 02:36:31
【问题描述】:
所有在 reactstrap modal 组件中创建的 ref 在 compoenentDIDMount 中都是空的,无论它们是如何创建的
这与 react-dom 版本 16.8.6 有关。我尝试使用 react.createRef() 和回调直接分配 ref。
import { Modal, Form } from 'reactstrap'
class QuickBidModal extends React.Component {
constructor(props) {
super(props);
this.state = {
isOpen: false,
};
this.input1 = React.createRef();
}
componentDidMount(){
console.log(this.input1) //Always returns current as null
}
render(){
<Modal>
<Form>
<div className="input" ref={this.input1} />
</Form>
</Modal>
}
我希望 this.input1 属于当前属性的输入类。但是,它只返回 null。如果我在 Modal 组件的外部创建 div 元素,则 ref 可以正常工作。
【问题讨论】:
-
图书馆似乎没有考虑传递给其孩子的帐户 ref。它必须进行某种克隆以添加一些自定义道具,如果您可以将其他道具传递给它的孩子,请尝试检查文档。
-
这里是源代码:github.com/reactstrap/reactstrap/blob/master/src/Form.js 你可以看到没有'this.props.children' 所以这意味着这个包没有考虑到孩子所以真正的问题是: 你到底能做到什么?如果我们查看源代码,他们总是使用标签道具并将其用作反应元素。所以也许你可以简单地将带有 ref 的标签道具传递给它。
-
@MaieonBrix 他们使用对象扩展语法将其余的道具(包括
children)放入attributes变量中,该变量将应用于别名Tag下的组件。所以Tag组件应该有children。 -
确实!谢谢@Rallen,那么也许我们可以使用 innerRef 道具?
标签: javascript reactjs reactstrap