javascript - React Native 设置 TextInput 值大写
问题描述
我是 React Native 的新手,不知道为什么它会显示出意外的结果。如果我输入“a”然后输入“a”,它会显示“AAA”,依此类推。
export default class App extends Component {
constructor(props) {
super(props)
this.state = {
userName: ''
}
}
formatUserName = (textValue) => {
// If I remove toUpperCase() below, it shows expected result.
this.setState({ userName: textValue.toUpperCase() });
}
render() {
...
<TextInput
onChangeText={textValue => this.formatUserName(textValue)}
value={this.state.userName} />
...
}
}
解决方案
如果要将输入字符串更改为大写字符串,则可以在 TextInput 中使用autoCapitalize道具。
<TextInput
autoCapitalize = {"characters"}
onChangeText={(text) => this.setState({ userName: text })}
value={this.state.userName} />
道具autoCapitalize有以下选项:
- 字符:所有字符。
- words:每个单词的第一个字母。
- 句子:每个句子的第一个字母(默认)。
- none:不自动大写任何内容。
默认值为句子
推荐阅读
- node.js - 使用私有解密(publicEncrypt、privateDecrypt)解密时出现错误解密错误
- nsis - NSIS 安装程序屏幕中的新自定义打印按钮未显示
- javascript - Thymeleaf JS 无法传递参数
- javascript - 重构javascript中的硬代码
- azure - IoT Workbench 中仅显示一个 Azure 订阅:Cloud Azure 配置命令
- angular - 如何对角度@Output 进行单元测试
- ios - Ionic Cordova Typescript iOS App - 自动从 BLE 检索数据
- css - 如何选择最后一个元素?
- javascript - 在 componentDidMount 或单击按钮时调用 API
- javascript - 在跨域 iframe 中获取 iframe id