Reputation: 592
First of all here is my custom component.
return (
<View style={[styles.container, this.props.style]}>
<TextField
style={styles.inputText}
autoCapitalize={this.props.autoCapitalize}
ref={this.props.ref}
autoCorrect={false}
onFocus={() => this.setState({isFocus:true})}
onBlur={() => this.setState({isFocus:false})}
value={this.state.text}
label={this.props.placeholder}
secureTextEntry={this.props.secureTextEntry}
blurOnSubmit={this.props.blurOnSubmit}
keyboardType={this.props.keyboardType}
returnKeyType={this.props.returnKeyType}
textContentType={this.props.textContentType}
// onSubmitEditing={this.props.focus(this.props.textInput)}
onSubmitEditing={this.props.onSubmit}
placeholderTextColor='grey'
onChangeText={this.props.onChangeText ? this.props.onChangeText : (text) => this.setState({ text })}
editable={this.props.editable}
/>
</View>
);
Now on submit the email field i want to make focus to password field and on submit on password i want to submit the form .
Here is the code for that :
<MaterialTextInput
placeholder="Email"
// ref={(input) => { this.emailInput = input; }}
ref={ref => (this.emailInput = ref)}
onSubmit = {() => this.passwordInput.focus()}
onChangeText={(text) => this.handleEmailchange(text)}/>
<MaterialTextInput
placeholder="Password"
ref={ref => this.passwordInput = ref}
onSubmit = {() => this.submit()}
onChangeText={(text) => this.handlePasswordchange(text)}/>
But this does not works . It gives error ,
this.passwordInput.focus is undefined
Please tell me what i am doing wrong here ?
Upvotes: 0
Views: 656
Reputation: 2514
You cannot use ref
directly in your custom component. Use React.forwardRef()
. Also don't use this.props.
but use props.
as your component isn't class-based.
Because your component
<MaterialTextInput>
inthis.passwordInput
is actually a<View>
not<TextField>
. And View don't have any method namedfocus()
!!!
Above isn't the case anymore.
1) Export your Component from its file e.g. MaterialTextInput.js
export default MaterialTextInput = React.forwardRef((props, ref) => (
<View style={[styles.container, props.style]}>
<TextField ref={ref}
onSubmitEditing={props.onSubmit}
...
/>
</View>
));
2) Then import you component:
import MaterialTextInput from 'MaterialTextInput';
3) And use your component as you did
...
<MaterialTextInput ref={ref => this.emailInput = ref}
onSubmit={() => this.passwordInput.focus()}
... />
<MaterialTextInput ref={ref => this.passwordInput = ref}
onSubmit={() => this.submit()}
... />
Upvotes: 0
Reputation: 5690
You cannot use ref
directly in your custom component. You have to declare your custom component as below :
const MaterialTextInput = React.forwardRef((props, ref) => {
return (
<View style={[styles.container, this.props.style]}>
<TextField
style={styles.inputText}
autoCapitalize={this.props.autoCapitalize}
ref={ref} // Change Here also
autoCorrect={false}
onFocus={() => this.setState({isFocus:true})}
onBlur={() => this.setState({isFocus:false})}
value={this.state.text}
label={this.props.placeholder}
secureTextEntry={this.props.secureTextEntry}
blurOnSubmit={this.props.blurOnSubmit}
keyboardType={this.props.keyboardType}
returnKeyType={this.props.returnKeyType}
textContentType={this.props.textContentType}
// onSubmitEditing={this.props.focus(this.props.textInput)}
onSubmitEditing={this.props.onSubmit}
placeholderTextColor='grey'
onChangeText={this.props.onChangeText ? this.props.onChangeText : (text) => this.setState({ text })}
editable={this.props.editable}
/>
</View>
)
})
Now, you can use ref in your component.
Upvotes: 2