当类型正确时,在本机反应中出现无效类型错误

gatting invalid type error in react native while type is correct

我正在使用 useState 来设置这样的值

  const [number, setNumber] = useState('');

这是我的意见

<TextInput
          style={styles.input}
          placeholder="Enter Number 03********"
          keyboardType="phone-pad"
          textAlign={'center'}
          selectionColor="#1e2d50"
          maxLength={11}
          value={number}
          onChange={(e) => setNumber(e)}
        />

当我进行输入时,出现此错误

 Failed prop type: Invalid prop `value` of type `object` supplied to `ForwardRef(TextInput)`, expected `string`.

如何解决?

在 react-native 中,textInput 值需要一个字符串而不是数字。如果您想提供一个数字作为值,您首先需要将其转换为字符串;

<TextInput ... value={String(number)} onChangeText={text => setNumber(text)} />