React Native-text input does not lose focus
And I Put a <TextInput/>
- to do with the dawn on my project, but after that I click on in the <TextInput/>
, and the focus is more on him, it doesn't matter where you click on the screen, the function of onBlur()
is no longer running and has no mistakes, even if I go to another page and return to the screen in that you have that <TextInput/>
, is the one bar that blinks when you go to type in, continues to flash, I've tried just several examples of the <TextInput/>
, and they all exhibit this same problem, there must be something very silly, but I can't solve it at all. Here's the code I'm testing right now.
import {
StatusBar,
View,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
SafeAreaView,
} from 'react-native';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
borderColor: '#fff',
};
}
onFocus() {
this.setState({
borderColor: '#000',
});
}
onBlur() {
this.setState({
borderColor: '#fff',
});
}
render() {
return (
<View
style={{
width: '100%',
height: '100%',
backgroundColor: '#363A44',
alignItems: 'center',
}}>
<StatusBar
barStyle="light-content"
hidden={false}
backgroundColor="#000"
/>
<TextInput
onBlur={() => this.onBlur()}
onFocus={() => this.onFocus()}
style={{
height: 40,
width: 100,
borderWidth: 1,
borderColor: this.state.borderColor,
}}
/>
</View>
);
}
}
export default App;```
1 answers
Your problem is recurring in React Native, it basically happens because the View can't detect the touches on the screen. Alternatively, you can swap your view master for a ScrollView configured to accept on-screen touches.
Baiscamamente just change:
<View
style={{
width: '100%',
height: '100%',
backgroundColor: '#363A44',
alignItems: 'center',
}}>
<StatusBar
barStyle="light-content"
hidden={false}
backgroundColor="#000"
/>
<TextInput
onBlur={() => this.onBlur()}
onFocus={() => this.onFocus()}
style={{
height: 40,
width: 100,
borderWidth: 1,
borderColor: this.state.borderColor,
}}
/>
</View>
By:
<ScrollView
style={{
width: '100%',
height: '100%',
backgroundColor: '#363A44',
alignItems: 'center',
}}
scrollEnabled={false}
keyboardShouldPersistTaps='handled'>
<StatusBar
barStyle="light-content"
hidden={false}
backgroundColor="#000"
/>
<TextInput
onBlur={() => this.onBlur()}
onFocus={() => this.onFocus()}
style={{
height: 40,
width: 100,
borderWidth: 1,
borderColor: this.state.borderColor,
}}
/>
</ScrollView>
ScrollEnabled : leaves the view static, without the scroll.
KeyboardShouldPersistTaps : enables the ability to touch the area outside the keyboard.