【发布时间】:2019-02-14 06:28:14
【问题描述】:
我想使用createRef() 将焦点放在下一个TextInput。我在createRef() 中遇到错误。我究竟做错了什么?提前致谢。
constructor(props) {
super(props);
this.r2Ref = React.createRef();
this.r3Ref = React.createRef();
this.r4Ref = React.createRef();
this.r5Ref = React.createRef();
}
render() {
return (
<View style={SetStyle.settingsCont}>
<ScrollView>
<View style={SetStyle.contRate}>
<View style={SetStyle.rView}>
<Text style={SetStyle.rText}>Rate1</Text>
<TextInput style={SetStyle.rInput} keyboardType='numeric'
returnKeyType="next" onSubmitEditing={() => this.refs.r2Ref.focus()}></TextInput>
</View>
<View style={SetStyle.rView}>
<Text style={SetStyle.rText}>Rate2</Text>
<TextInput style={SetStyle.rInput} keyboardType='numeric'
ref={r2Ref => this.r2Ref = r2Ref}
returnKeyType="next" onSubimitEditing={() => this.refs.r3Ref.focus()}></TextInput>
</View>
<View style={SetStyle.rView}>
<Text style={SetStyle.rText}>Rate3</Text>
<TextInput style={SetStyle.rInput} keyboardType='numeric'
ref={r3Ref => this.r3Ref = r3Ref}
returnKeyType="next" onSubmitEditing={() => this.refs.r4Ref.focus()}></TextInput>
</View>
<View style={SetStyle.rView}>
<Text style={SetStyle.rText}>Rate4</Text>
<TextInput style={SetStyle.rInput} keyboardType='numeric'
ref={r4Ref => this.r4Ref = r4Ref}
returnKeyType="next" onSubmitEditing={() => this.refs.r5Ref.focus()}></TextInput>
</View>
<View style={SetStyle.rView}>
<Text style={SetStyle.rText}>Rate5</Text>
<TextInput style={SetStyle.rInput} keyboardType='numeric'
ref={r5Ref => this.r5Ref = r5Ref}></TextInput>
</View>
</View>
</ScrollView>
</View>
);
}
}
我收到以下错误:
未定义不是对象(评估 this2.refs.r2Refs.focus)
【问题讨论】:
-
您能否添加您收到的错误消息
-
另外,你不应该在 state 中定义 refs。将它们定义为类变量。
-
我也定义了状态外的引用。但是我也遇到了错误
-
而不是 onSubmitEditing={() => this.refs.r2Ref.focus()} 。您是否尝试过如下 onSubmitEditing={() => this.r2Ref.current.focus()}
标签: reactjs react-native textinput