首先,这是我的自定义组件。
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>
);
现在,在提交电子邮件字段时,我要关注密码字段;在提交密码字段时,我要提交表单。
这是该代码:
<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)}/>
但这不起作用。 它给出了错误,
this.passwordInput.focus is undefined
请告诉我我在做什么错了吗?
答案 0 :(得分:1)
您不能直接在自定义组件中使用ref
。您必须声明您的自定义组件,如下所示:
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>
)
})
现在,您可以在组件中使用ref。
答案 1 :(得分:0)
您不能直接在自定义组件中使用ref
。使用React.forwardRef()
。另外,请勿使用this.props.
,而应使用props.
,因为您的组件不是基于类的。
因为
<MaterialTextInput>
中的组件this.passwordInput
实际上是<View>
而不是<TextField>
。而且View没有任何名为focus()
的方法!
以上不再是这种情况。
1)从其文件中导出 Component 。 MaterialTextInput.js
export default MaterialTextInput = React.forwardRef((props, ref) => (
<View style={[styles.container, props.style]}>
<TextField ref={ref}
onSubmitEditing={props.onSubmit}
...
/>
</View>
));
2)然后导入您的组件:
import MaterialTextInput from 'MaterialTextInput';
3)并像以前一样使用组件
...
<MaterialTextInput ref={ref => this.emailInput = ref}
onSubmit={() => this.passwordInput.focus()}
... />
<MaterialTextInput ref={ref => this.passwordInput = ref}
onSubmit={() => this.submit()}
... />