【问题标题】:Focus to next text input or submit not working in custom text input关注下一个文本输入或提交在自定义文本输入中不起作用
【发布时间】:2020-03-01 15:21:40
【问题描述】:

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

 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

请告诉我我在这里做错了什么?

【问题讨论】:

  • 因为this.passwordInput 中的组件&lt;MaterialTextInput&gt; 实际上是&lt;View&gt; 而不是&lt;TextField&gt;。而 View 没有任何名为 focus() 的方法!!!

标签: reactjs react-native


【解决方案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。

【讨论】:

  • 你是否也改变了这一行 ref={ref}
  • 未定义不是对象materialTextInput
【解决方案2】:

您不能在自定义组件中直接使用ref。使用React.forwardRef()。也不要使用this.props.,而是使用props.,因为你的组件不是基于类的。

因为this.passwordInput 中的组件&lt;MaterialTextInput&gt; 实际上是&lt;View&gt; 而不是&lt;TextField&gt;。而 View 没有任何名为 focus() 的方法!!!

不再是上述情况了。

1) 从文件中导出您的 组件,例如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()}
... />

【讨论】:

  • 我评论了 View,现在主要是 TextField,但这也不起作用
  • 另外,如果我想将文本输入和文本放在同一个组件中的同一个视图中,那么该怎么做
  • 刚刚添加了React.forwardRef,就像@K.Bharda 的回答一样。检查一下!
  • 找不到变量 MaterialTextInput
  • 你在哪里使用你的组件?你是在同一个文件中还是你导入了组件???
猜你喜欢
  • 1970-01-01
  • 2019-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多