Failed prop type: 传递给 ‘TextInput’ 的无效 prop 类型 ‘object’ 的 ‘value’ prop

huangapple go评论72阅读模式
英文:

Failed prop type: Invalid prop 'value' of type 'object' supplied to 'TextInput'

问题

当我将输入更改为''并提交它(在TextInput中),我会遇到以下错误:"属性类型不匹配:无效的属性'value',类型为'对象',提供给'TextInput'"。

<TextInput 
  style={styles.input} 
  autoCapitalize='none'
  onChange={email => this.setState({email})}
  value={this.state.email}
>
英文:

When I change the input to '' and submit it (in the TextInput) I have the following error: "Failed prop type: Invalid prop 'value' of type 'object' supplied to 'TextInput'"

        &lt;TextInput 
          style={styles.input} 
          autoCapitalize=&#39;none&#39;
          onChange={email =&gt; this.setState({email})}
          value={this.state.email}
        &gt;

答案1

得分: 1

你的 onChange() 方法应该如下所示:

        &lt;TextInput 
          style={styles.input} 
          autoCapitalize=&#39;none&#39;
          onChange={(e) =&gt; {
            this.setState({
              email: e.nativeEvent.text
            })
          }}
          value={this.state.email}
        &gt;

或者你可以使用 onChangeText() 将输入的文本分配给你的状态,如下所示:

&lt;TextInput 
          style={styles.input} 
          autoCapitalize=&#39;none&#39;
          onChangeText={(email) =&gt; this.setState({ email })}
          value={this.state.email}
        &gt;

更多文档在此

英文:

Your onChange() method should be as follow:

        &lt;TextInput 
          style={styles.input} 
          autoCapitalize=&#39;none&#39;
          onChange={(e) =&gt; {
            this.setState({
              email: e.nativeEvent.text
            })
          }}
          value={this.state.email}
        &gt;

Or you can just use onChangeText() to assing entered text into your state as below :

&lt;TextInput 
          style={styles.input} 
          autoCapitalize=&#39;none&#39;
          onChangeText={(email) =&gt; this.setState({ email })}
          value={this.state.email}
        &gt;

More doc here.

答案2

得分: 0

如果您已经像这样定义了您的状态:

state={email:''} // 或者
this.state={email:''}

更改的代码如下:

<TextInput 
  style={styles.input} 
  autoCapitalize='none'
  onChange={e => this.setState({email: e.target.value})}
  value={this.state.email}
>
英文:

if you have defined your state like this

     state={email:&#39;&#39;} // or
     this.state={email:&#39;&#39;}

changed code

    &lt;TextInput 
      style={styles.input} 
      autoCapitalize=&#39;none&#39;
      onChange={e=&gt; this.setState({email:e.target.value})}
      value={this.state.email}
      &gt;

huangapple
  • 本文由 发表于 2020年1月6日 19:01:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定