警告:道具类型失败:提供给“TextInput”的“number”类型的道具“value”无效,应为“string”

Warning: Failed prop type: Invalid prop `value` of type `number` supplied to `TextInput`, expected `string`

在本机反应中,我有:

Warning: Failed prop type: Invalid prop `value` of type `number` supplied to `TextInput`, expected `string`.

我有一个postalCode,它是数值。

我已经在 <TextInput /> 上设置了 keyboardType="numeric" 但我在 ios/android/web 上仍然有这个错误。

我该如何解决?

只需将您的数字转换为字符串

<TextInput value={postalCode.toString()} ...

keyboardType 更改为 numeric 不会使您的 TextInput 仅接受数字,它只会更改移动设备上的键盘布局。使用 keyboardType=numeric 您的键盘将只有数字,以便用户更容易键入数字,这是一个用户体验问题,但它不会使您的 TextInput 成为数字类型,这就是您看到此警告的原因.

我认为问题出在 onChange 回调中。

您可以这样调用 onChangeText 方法:

onChangeText: (text) => setValue(text)

希望有用!