I'm new in React Native and not sure why it shows unexpected result. If I type 'a' then 'a' it shows 'AAA', and so on.
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 basics You can make your element look even better like so: const styles = StyleSheet. create({ input: { borderColor: "gray", width: "100%", borderWidth: 1, borderRadius: 10, padding: 10, }, }); In the piece of code above, we styled the text box's border and gave it some padding.
It can be done using React. Approach: Using event listener, we will change lower case values to uppercase. When user begins to type, an onChange event is created, we are updating the state of value with the upperCase value of the entered value using toUpperCase() function.
We are going to use onContentSizeChange prop provided by TextInput to watch input box for content size changes and accordingly call our updateSize function which will update the height of our TextInput. In our render process we are going to use the height to create anew style to use with our TextInput.
If you want to change input string to uppercase string, then you can use autoCapitalize props in TextInput.
<TextInput
autoCapitalize = {"characters"}
onChangeText={(text) => this.setState({ userName: text })}
value={this.state.userName} />
Props autoCapitalize has following options:
Default value is sentences
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With