React-Native refs undefined on text input

23,062

Try setting the reference using a function. Like this:

<TextInput ref={(ref) => { this.FirstInput = ref; }} />

Then you can access to the reference with this.FirstInput instead of this.refs.FirstInput

Share:
23,062
wdlax11
Author by

wdlax11

Updated on July 05, 2022

Comments

  • wdlax11
    wdlax11 almost 2 years

    we are currently running React-Native 0.33

    We are trying to use the refs to go from 1 text input to another when they hit the next button. Classic username to password.

    Anyone have any idea? Below is the code we are using. From other posts I've found on stack this is what they've done; however, it's telling us that this.refs is undefined.

    UPDATE So I've narrowed the problem down to

     render() {
        return (
          <Navigator
              renderScene={this.renderScene.bind(this)}
              navigator={this.props.navigator}
    
              navigationBar={
                <Navigator.NavigationBar style={{backgroundColor: 'transparent'}}
                    routeMapper={NavigationBarRouteMapper} />
              } />
        );
      }
    

    If I just render the code below in the renderScene function inside of the original render it works, however with the navigator it won't work. Does anyone know why? Or how to have the navigator show as well as render the code in renderScene to appear in the original render?

       class LoginIOS extends Component{
    
      constructor(props) {
        super(props);
        this.state={
          username: '',
          password: '',
          myKey: '',
        };
      }
    
      render() {
        return (
          <Navigator
              renderScene={this.renderScene.bind(this)}
              navigator={this.props.navigator}
    
              navigationBar={
                <Navigator.NavigationBar style={{backgroundColor: 'transparent'}}
                    routeMapper={NavigationBarRouteMapper} />
              } />
        );
      }
    
      renderScene() {
        return (
          <View style={styles.credentialContainer}>
                    <View style={styles.inputContainer}>
                      <Icon style={styles.inputPassword} name="person" size={28} color="#FFCD00" />
                          <View style={{flexDirection: 'row', flex: 1, marginLeft: 2, marginRight: 2, borderBottomColor: '#e0e0e0', borderBottomWidth: 2}}>
                            <TextInput 
                                ref = "FirstInput"
                                style={styles.input}
                                placeholder="Username"
                                autoCorrect={false}
                                autoCapitalize="none"
                                returnKeyType="next"
                                placeholderTextColor="#e0e0e0"
                                onChangeText={(text) => this.setState({username: text})}
                                value={this.state.username}
                                onSubmitEditing={(event) => {
                                  this.refs.SecondInput.focus();
                                }}
                                >
    
                            </TextInput>
                          </View>
                    </View>
    
                    <View style={styles.inputContainer}>
                      <Icon style={styles.inputPassword} name="lock" size={28} color="#FFCD00" />
                          <View style={{flexDirection: 'row', flex: 1, marginLeft: 2, marginRight: 2, borderBottomColor: '#e0e0e0', borderBottomWidth: 2}}>
                            <TextInput
                                ref = "SecondInput"
                                style={styles.input}
                                placeholder="Password"
                                autoCorrect={false}
                                secureTextEntry={true}
                                placeholderTextColor="#e0e0e0"
                                onChangeText={(text) => this.setState({password: text})}
                                value={this.state.password}
                                returnKeyType="done"
                                onSubmitEditing={(event)=> {
                                  this.login();
                                }}
                                focus={this.state.focusPassword}
                                >
                            </TextInput>
                          </View>
                    </View>
          </View>
          );
      }