【问题标题】:Failed prop type invalid prop 'value'失败的道具类型无效的道具'值'
【发布时间】:2017-07-11 16:28:30
【问题描述】:

我在 react-native 中引入了用于搜索的 Texinput。 以下是代码:

      constructor(props){
        super(props);
        this.state = {
          value : "",
          items: [],
        }

        this.handleHeaderSearch = this.handleHeaderSearch.bind(this);
        this.handleSearchBtn = this.handleSearchBtn.bind(this);
      }
      handleSearchBtn(){

      }
      handleHeaderSearch(){
        if(!this.state.value) return;

  }

和:

<TextInput
  value={this.props.value}
  onChange = {this.props.onChange}
  placeholder={"جستجو"}
  blurOnSubmit={false}
  style={{flex:0.9}}
  returnKeyType="done"
/>

每当我在输入文本后运行 Android 时,我都会看到以下警告:

“警告失败的道具类型无效的道具'值'类型'对象'提供给TextInput,预期'字符串'”

【问题讨论】:

  • 在更改值时可以发布代码 sn-p 吗?
  • 它工作正常,只有警告

标签: react-native


【解决方案1】:

您将值存储在 this.state.value 中(或者至少这似乎是您的意图),但您将 this.props.value 传递给您的 TextInput。

如果您确实打算将this.props.value 传递给TextInput,那么了解传递给该组件的内容将有所帮助(上一级)。

【讨论】:

    【解决方案2】:

    在某些情况下,当您尝试输入 number 而不是 string 类型的值时,会出现您提到的错误或 Warning: Failed prop type: Invalid prop value of type number supplied to TextInput, expected string。 react-native 的最佳实践是使用 String() 函数包装输入值,如下所示:

    <TextInput
      value={String(this.props.value)}
      onChange = {this.props.onChange}
      placeholder={"جستجو"}
      blurOnSubmit={false}
      style={{flex:0.9}}
      returnKeyType="done"
    />
    

    这是解决警告的方法:失败的道具类型:提供给“TextInput”的“数字”类型的无效道具“值”,预期为“字符串”。

    【讨论】:

      【解决方案3】:

      我在输入输入时遇到同样的错误,如果有人使用这样的功能组件,通过将 useState('') 设置为空字符串对我有用(如果 useState 设置为 null 或 @987654322 @)。

      转换value={String(text)} 对我不起作用,当我尝试将文本的值转换为字符串时,它会在输入占位符中显示[object, object]

      const HomeContainer = () => {
        const [text, onChangeText] = useState(''); // Just set useState() to empty string ('')
          
              return (
                <>
                <SafeAreaView>
                  <ScrollView styles={styles.scrollView}>
                  <Text style={styles.welcome}>Verify Faces</Text>
                  <Text></Text>
                  <View
                  style={[
                    styles.container,
                    {backgroundColor: COLORS.primaryLight}
                  ]}>
                      <TextInput style={styles.input} value={text} onChange={onChangeText} placeholder='Enter your name' placeholderTextColor = "#000"/>
                      <TextInput style={styles.input} value={text} type='email' onChange={onChangeText} placeholder='Enter your email' placeholderTextColor = "#000"/>
                    </View>
                    <SimpleImagePicker />
                  </ScrollView>
                </SafeAreaView>
                </>
              );
      }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-27
        • 2017-07-31
        • 1970-01-01
        • 1970-01-01
        • 2019-02-07
        • 2019-05-17
        • 2017-12-23
        • 2021-01-18
        相关资源
        最近更新 更多