0
有一个主React组件和一个名为AttributeInput
的子组件。 为了避免重复代码,我提取一些代码从我的主要成分,并把它放在一个方法从AttributeInput
未提供反应参考子组件
我试图引用这样的代码:
{this.state.attributeInput ? <AttributeInput ref="attributeInput"/> : null}
<div>
{this.refs["attributeInput"].displayInputField(
"List Name",
"List Name(<15 characters - A-Z, 0-9 and _ only)",
this.setListName
)}
{this.refs["attributeInput"].displayInputField(
"List Description",
"List Description",
this.setListDescription
)}
</div>
然而,如果this.state.attributeInput
是假,AttributeInput
将不会呈现。因此,当我在这里引用它,
this.refs["attributeInput"].displayInputField(
"List Name",
"List Name(<15 characters - A-Z, 0-9 and _ only)",
this.setListName
)
我得到一个错误,说AttributeInput
是不确定的。
如果未呈现,我可以参考AttributeInput
吗? 我的方法有其他选择吗?
您无法在'render'本身内部访问'refs'到其他组件。无论如何'AttributeInput'的目的是什么?你为什么要调用它的方法?你是否滥用React组件作为方法包? –