0

I am very new in react Native world. I want to shift focus from the first input box to second input box. Please find my below code.

<View style={styles.inputViewStyle}>
                  <TextInput
                    ref={firstnameRef => (this.firstnameRef = 
                    firstnameRef)}
                    label="Firstname"
                    returnKeyType="next"
                    autoCorrect={false}
                    value={this.state.firstname}
                    onSubmitEditing={() => 
              this.refs.middlenameRef.focus()}
                    onChangeText={firstname => this.setState({ firstname })}
                    blurOnSubmit={false}
                  />
                </View> 

    <View style={styles.inputViewStyle}>
                  <TextInput
                    ref={middlenameRef => (this.middlenameRef = middlenameRef)}
                    label="Middlename"
                    returnKeyType="go"
                    value={this.state.middlename}
                    onChangeText={middlename => this.setState({ middlename })}
                  />
                </View>

I am getting error -> "undefined is not an object(evaluating 'this2.middlenameRef.focus()')"

UPDATE: constructor(props) { super(props); this.state = { firstname: "", middlename: "", lastname: ""
};

Please guide. Thanks in advance.

SaurabhG
  • 173
  • 1
  • 11
  • Possible duplicate of [React Native: How to select the next TextInput after pressing the "next" keyboard button?](https://stackoverflow.com/questions/32748718/react-native-how-to-select-the-next-textinput-after-pressing-the-next-keyboar) – Quoc Nguyen Oct 22 '18 at 04:57
  • Can you provide your constructor code? – HungrySoul Oct 22 '18 at 04:57
  • HungrySoul: Please check. @QuocNguyen - I have tried that solutiion but somehow it is not working for me. – SaurabhG Oct 22 '18 at 05:08

4 Answers4

1

Set the second TextInput focus, when the previous TextInput's onSubmitEditing is triggered.

Try this

  1. Adding a Ref to second TextInput ref={(input) => { this.secondTextInput = input; }}

  2. Bind focus function to first TextInput's onSubmitEditing event. onSubmitEditing={() => { this.secondTextInput.focus(); }}

  3. Remember to set blurOnSubmit to false, to prevent keyboard flickering. blurOnSubmit={false}

1
<TextInput
    placeholder = "FirstTextInput"
    returnKeyType = { "next" }
    onSubmitEditing={() => { this.secondTextInput.focus(); }}
    blurOnSubmit={false}
/>
<TextInput
    ref={(input) => { this.secondTextInput = input; }}
    placeholder = "secondTextInput"
/>
0

Try this:

onSubmitEditing={() => { this.middlenameRef.focus() }}
Uzair A.
  • 1,586
  • 2
  • 14
  • 30
0

You can get instance directly through this. And null Checking could be a good option for debug.

onSubmitEditing={() => this.middlenameRef && this.middlenameRef.focus()}
Jeff Gu Kang
  • 4,749
  • 2
  • 36
  • 44