我有一个像这样的组件包装器:
<SpecialWrapper config={}>
{this.props.children}
</SpecialWrapper>
想法是像这样使用它:
<SpecialWrapper>
// whatever other components you have
<SpecialInput1/>
<SpecialInput1/>
<Button onClick={()=> SpecialWrapper.methodWhereValuesNeeded()}
</SpecialWrapper>
是否可以通过“ methodWhereValuesNeeded”方法访问特殊输入的值?
答案 0 :(得分:1)
您可以在包装器组件中管理specialInput的状态,并将用于更新状态的函数作为道具传递给特殊Input
functionForUpdatingWrapperState = (newValue) => {
this.state.SpecialInput1Value = newValue;
}
<SpecialWrapper>
// whatever other components you have
<SpecialInput1/>
<SpecialInput1 updateValueFunction={functionForUpdatingWrapperState}/>
<Button onClick={()=> SpecialWrapper.methodWhereValuesNeeded()}
</SpecialWrapper>