如何在不使用 ref 的情况下专注于文本输入



我尝试使用对焦和自动对焦。使用 React Native 0.43示例简单方案

<ListItem
 title="Username"
 textInput={true}
 textInputValue={this.state.userName}
 textInputSecureTextEntry={false}
 textInputOnChangeText={this
 .bindData
 .bind(this, 'userName')}
 textInputAutoFocus={this.state.userNameAutoFocus}
 textInputFocus={this.state.userNameFocus}
 textInputKeyboardType="default"
 textInputPlaceHolder="Type your Username Here"
 textInputContainerStyle={styles.inputFormStyle}
 textInputOnSubmitEditing={this
 .nextFormComponent
 .bind(this, 'userPassword')}
 textInputReturnKeyType="next" /> 

源代码源代码 NPM

constructor(props) {
  super(props);
  this.state = { 
    textInputFocus:true
  };
}
render() {
 <View>
   <TextInput autoFocus={this.state.textInputFocus} />
 </View>
} 

试试这段代码。

你可以试试:

class App extends React.Component{
  componentDidMount(){
    this.nameInput.focus();
  }
  render() {
    return(
      <View>
        <input defaultValue="Won't focus"/>
        <input ref={(input) => { this.nameInput = input; }}   defaultValue="will focus"  />
      </View>
    );
  }
}

相关内容

最新更新