Modal dismissing keyboard crash
See original GitHub issueMac OSX react-native: 0.24.1
In the following component when clicking Cancel I get the following in the XCode console and the Modal closes then opens and then freezes.
Warning: Attempt to dismiss from view controller
<UIViewController: 0x7fa152e17ca0>
while a presentation or dismiss is in progress!
'use strict';
import React, {
Component,
View,
Text,
TextInput,
TouchableOpacity,
Dimensions,
DeviceEventEmitter,
LayoutAnimation,
} from 'react-native';
class Example extends Component {
constructor(props) {
super(props)
var dimensions = Dimensions.get('window')
this.state = {
notes: '',
width: dimensions.width,
height: dimensions.height,
}
}
componentWillMount() {
DeviceEventEmitter.addListener('keyboardWillShow', this.keyboardWillShow)
DeviceEventEmitter.addListener('keyboardWillHide', this.keyboardWillHide)
}
componentWillUnmount() {
DeviceEventEmitter.removeAllListeners('keyboardWillShow')
DeviceEventEmitter.removeAllListeners('keyboardWillHide')
}
keyboardWillShow = (e)=> {
let height = Dimensions.get('window').height - e.endCoordinates.height - headerHeight
LayoutAnimation.easeInEaseOut()
this.setState({ height })
}
keyboardWillHide = (e)=> {
let height = Dimensions.get('window').height - headerHeight
LayoutAnimation.easeInEaseOut()
this.setState({ height })
}
open() {
this.setState({ open: true })
}
close() {
this.setState({ open: false })
}
save() {
this.setState({ open: false })
}
render() {
return (
<ScrollView keyboardShouldPersistTaps={ true }>
<TouchableOpacity onPress={()=> this.open() }>
<Text>Open</Text>
</TouchableOpacity>
<Modal
animationType="slide"
visible={ this.state.open }
style={{ flex: 1 }}
>
<TouchableOpacity onPress={()=> this.close() }>
<Text>Cancel</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=> this.save() }>
<Text>Save</Text>
</TouchableOpacity>
<TextInput
width={this.state.width}
height={this.state.height}
defaultValue={ this.state.notes }
onChangeText={(notes)=> this.setState({ notes }) }
autoFocus={ true }
multiline={ true }
/>
</Modal>
</ScrollView>
)
}
}
When I remove keyboardShouldPersistTaps={ true }
from <ScrollView>
it doesn’t crash but that’s there so that clicks on these buttons works when the input has focus.
The only fix I have found so far to this issue is to dismiss the keyboard and then proceed in a timeout.
close() {
dismissKeyboard()
setTimeout(()=> {
this.setState({ open: false })
})
}
Fact: setTimeout
literally fixes everything
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:15 (6 by maintainers)
Top Results From Across the Web
iOS 9 app crash on keyboard dismiss | Apple Developer Forums
We are using a custom keyboard input accessory, and the textView in question is within a collectionView cell. This crash is not reproducible...
Read more >My app crash everytime I add code to dismiss keyboard - Swift
The keyboard is dismissed as soon I touch anywhere on the screen as is suppose to without crash the app.
Read more >react-native-modal - npm
Start using react-native-modal in your project by running `npm i ... avoidKeyboard, bool, false, Move the modal up if the keyboard is open....
Read more >[Answer]-SwiftUI randomly crashes on presentationMode ...
Coding example for the question SwiftUI randomly crashes on presentationMode?.wrappedValue.dismiss()-swift.
Read more >Today's React Native Tip: Keyboard issues in ScrollView
Fix: keyboardShouldPersistTaps · 'never' (the default), tapping outside of the focused text input when the keyboard is up dismisses the keyboard. When this ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
0.40.0 has the same issue; closing a modal after a keyboard was used causes the modal to jump back up and crash the app.
Found a really great ‘work around.’ Although, probably best practice to do it this way all along.
Edit: the above chunk of code is ran through when onSubmitEditing fire’s off an a TextInput.