javascript - 提供给 textinput 的类型 number 的无效 prop 值预期字符串 react native

标签 javascript reactjs react-native react-redux

我正在 native react 中为文本输入创建字数统计,我认为我已经能够实现字数统计,但是,当我输入文本输入时,我收到“数字类型的无效 Prop 值”的错误提供给文本输入预期的字符串 react native '。我已经尝试了一切,但没有成功

        constructor(props) {
          super(props);
          this.state = {
            ModalVisibleStatus: false,
            maxLength: 70,
            charLength: 70,
            messageValue: '',
          };
        }


        onChangeMessage(messageValue) {
          const input = messageValue;
          const charLength = this.state.maxLength - input.length.toString();
          this.setState({
            charLength,
            messageValue: charLength
          });
        }


        <TextInput
          placeholder={placeholder}
          onChangeText={this.onChangeMessage.bind(this)}
          value={this.state.messageValue}
          autoFocus
          autoCorrect
          multiline
          maxLength={this.state.maxLength}
          style={styles.textInputStyle}
          underlineColorAndroid='transparent'
        />
        <Text style={{ textAlign: 'right', right: 5, }}>{this.state.messageValue}</Text>

最佳答案

您可以在 onChangeMes​​sage 函数中使用数值更新状态。

有两种方法可以纠正它。

  1. 以这种方式更新您的 onChangeMes​​sage 函数

    onChangeMes​​sage(messageValue) { 常量输入=消息值; const charLength = this.state.maxLength - input.length.toString(); this.setState({ 字符长度, 消息值:输入 }); }

  2. 更新状态时以这种方式在 ${charLength} 中写入 charLength。

我认为您通过用长度更新输入值来做错事。您应该使用用户在输入字段中输入的内容来更新输入值

关于javascript - 提供给 textinput 的类型 number 的无效 prop 值预期字符串 react native ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57752744/

相关文章:

javascript - Laravel 5.4 Mix 如何修改/丑化/混淆脚本

javascript - EventSource 监听器不会在 Firefox 中的页面加载时触发

javascript - 导入 react-native-fetch-blob 时出现意外 token

javascript - 通过 ID 在 JSON 中查找我的对象

Javascript 在日期对象本身中设置时区,如 setTimeOffset()

javascript - 在不使用jquery的情况下将g标签添加到svg标签中

javascript - react 覆盖对象中的状态变量

reactjs - 无法读取 null 的属性 'setState' - React.js、Modal、Bootstrap

Android 构建不起作用(在模块中发现重复的类 com.google.android.gms.internal.measurement.zzbt)

javascript - 上传到 Cloudinary API - 文件参数无效