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

时间:2017-04-09 13:25:26

标签: javascript react-native

我尝试使用焦点和自动对焦。使用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" /> 

Source Code Source Code NPM

2 个答案:

答案 0 :(得分:0)

constructor(props) {
  super(props);
  this.state = { 
    textInputFocus:true
  };
}

render() {
 <View>
   <TextInput autoFocus={this.state.textInputFocus} />
 </View>
} 

试试这段代码。

答案 1 :(得分:0)

你可以尝试:

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>
    );
  }
}