![](/img/trans.png)
[英]React Native SearchBar Error: Failed prop type: Invalid prop `value` of type `array` supplied to `ForwardRef(TextInput)`, expected `string`
[英]invalid prop value of type number supplied to textinput expected string react native
我在 react native 中为 textinput 创建了一个字数统计,我想我已经能够实现字数统计了,但是,当我输入 textinput 时,我收到一个错误“提供给 textinput 的类型数字的无效道具值”预期的字符串反应原生'。 我已经尽我所能,但无济于事
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 input = messageValue; const charLength = this.state.maxLength - input.length.toString(); this.setState({ charLength, messageValue: input }); }
在更新状态时以这种方式在${charLength}
写入 charLength。
而且我认为您通过使用长度更新输入值来做错了什么。 您应该使用用户在输入字段中输入的内容更新输入值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.