我正在 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>
最佳答案
您可以在 onChangeMessage 函数中使用数值更新状态。
有两种方法可以纠正它。
以这种方式更新您的 onChangeMessage 函数
onChangeMessage(messageValue) { 常量输入=消息值; const charLength = this.state.maxLength - input.length.toString(); this.setState({ 字符长度, 消息值:输入 }); }
更新状态时以这种方式在
${charLength}
中写入 charLength。
我认为您通过用长度更新输入值来做错事。您应该使用用户在输入字段中输入的内容来更新输入值
关于javascript - 提供给 textinput 的类型 number 的无效 prop 值预期字符串 react native ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57752744/