【发布时间】:2021-01-23 05:47:36
【问题描述】:
我想为同一个全局组件创建多个引用。例如 TextInput ,如果我想向它添加 ref 我使用。这在我的基于类的组件中:
<TextInput
ref={input => {
this.secondTextInput = input;
}}
/>
然后通过this.secondTextInput.focus(); 调用以聚焦文本输入。只要我将整个 textInput 直接导入我的班级,它就可以工作。
如果我在另一个文件中为 TextInput 创建了一个全局组件,例如:
export const OTPInput = props => {
return (
<TextInput
placeholder={props.title}
onChangeText={props.onTextEnter}
value={props.value}
/>
);
};
并在我的课程中使用它,方法是像这样导入:
Class ABC extends Component{
render(){
return(
<>
<OTPInput title ="first otp" />
<OTPInput title ="another otp" />
</>
)
}
}
如何创建 ref 并传递它,以便我可以通过单击类 func 中的某个按钮来聚焦文本输入。
任何帮助都会很棒
【问题讨论】:
标签: reactjs react-native