我有一段简单的代码,使用了一个TextInput
组件。我希望在它第一次渲染以及提交后,能够自动获取焦点。但是实际运行时,它并没有获得焦点。
render() {
return (
<TextInput
ref={(c) => this._input = c}
style={[styles.item, this.props.style]}
placeholder={"New Skill"}
onChangeText={(text) => {
this.setState({text})
}}
onSubmitEditing={(event) => {
this.props.onSubmitEditing(this.state.text);
this._input.clear();
this._input.focus();
}}
/>
);
}
componentDidMount() {
this._input.focus();
}
ref={(c) => this._input = c}
更改为ref={(c) => { this._input = c }}
,然后查看是否有效。 - bennygenelsetTimeout
函数内,如下:setTimeout(() => this._input.focus(), 250);
- I Putu Yoga Permana