简体   繁体   English

在反应本机文本输入中获取NaN

[英]getting NaN in react native text input

I am trying to parcel the value of one text input into another text input with a restricted maxLength of 2, when I type in a number and clear it, I am getting NaN, it does not revert back to the original state of 0. However, I also want the user to be able to clear out the textinput completely thereby leaving the textinput with just the placeholder 我试图将一个文本输入的值打包为maxLength限制为2的另一文本输入,当我键入数字并将其清除时,我得到的是NaN,它不会恢复为原始状态0。 ,我还希望用户能够完全清除textinput,从而使textinput只保留一个占位符

below is my code 下面是我的代码

      this.state = {
        total: 0,
      }


      onChangeTotal(number) {
        const total = parseInt(number);
        this.setState({ total });
      }



      <InputField
        value={this.state.total.toString()}
      />
      <InputField
        children={'Number of shoes'}
        iconType={'ios-basket'}
        placeholder={'Enter number of shoes'}
        keyboardType={'phone-pad'}
        maxLength={2}
        onChangeText={this.onChangeTotal}
        value={this.state.total.toString()}
      />

Check the length value of the number to see if the text is present. 检查数字的长度值以查看文本是否存在。

 onChangeTotal(number) {
        const total = parseInt(number);
        if(number.length === 0) {
          this.setState({ total: '' });
        } else {
         this.setState({ total });
        }
      }
<input value={this.state.total} type="number" min="0" max="99" onChange={(e) => this.setState({ total: e.target.value })} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM