I'd like to highlight the currently selected TextInput by changing the borderColor. I'm aware of the onFocus and onBlur properties, however, I can't figure out how to actually change the borderColor.
const MainScreen = () => {
const [pelletWeight, setPelletWeight] = React.useState('');
onFocus = () => {
console.log("Text is focused")
}
onBlur = () => {
console.log("Text is blurred")
}
return (
<View style={styles.root}>
<TextInput
style = {styles.container}
keyboardType='numeric'
placeholder="Pellet weight (grains)"
onChangeText={setPelletWeight}
value={pelletWeight}
onFocus={onFocus}
onBlur={onBlur}
/>
)
}
Any help would be much appreciated, thank you!
First you need a piece of state to keep track of whether the input is focused or not. Lets called it
isFocused. UseonBlurandonFocusto set its values. Now you create your focused textinput style. It will be added to the style only whenisFocusedis true:Demo
Now if you wanted to take things a step further, instead of the border width, border color, and background color instantly changing, you could use react native reanimated to make the transition happen smoothly:
Demo