use*_*830 0 typescript react-native native-base
我正在使用 React Native NativeBase,我想将一个数字传递给我的输入值,但我收到一个错误,提示我无法将数字传递给字符串。
<Input
keyboardType="numeric"
onChangeText={(text) => setTarget(text)}
value={target}
/>
Run Code Online (Sandbox Code Playgroud)
现在如果我这样做。
<Input
keyboardType="numeric"
// onChangeText={(text) => setTarget(text)}
value={"target"}
Run Code Online (Sandbox Code Playgroud)
错误消失。这是值的定义
const [target, setTarget] = useState<number>(0);
Run Code Online (Sandbox Code Playgroud)
You declared your target state as a number and the argument in onChangeText is always will be string. So first assign that number state as a string in input and when changes happen in value convert it to number as below :
<Input
keyboardType="numeric"
onChangeText={(text) => setTarget(parseInt(text, 10))} // conver to number
value={target.toString()} // convert to string
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2139 次 |
| 最近记录: |