专注于下一个文本输入或提交在自定义文本输入中不起作用的

首先,这是我的自定义组件。

 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

请告诉我我在做什么错了吗?

taidengmeidian 回答:专注于下一个文本输入或提交在自定义文本输入中不起作用的

您不能直接在自定义组件中使用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。

,

您不能直接在自定义组件中使用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()}
... />
本文链接:https://www.f2er.com/3160876.html

大家都在问