question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Modal dismissing keyboard crash

See original GitHub issue

Mac 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:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
rkhocommented, Jan 23, 2017

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.

1reaction
mini-eggscommented, Jan 23, 2017

Found a really great ‘work around.’ Although, probably best practice to do it this way all along.

import { Keyboard } from 'react-native'
...
Keyboard.removeAllListeners('keyboardDidHide') 
// not totally necessary and may be harmful if you do this in more than one location, works for my case
Keyboard.addListener('keyboardDidHide', () => { this.checkSubmit() })
// checkSubmit is a function the pulls up the modal
Keyboard.dismiss() 

Edit: the above chunk of code is ran through when onSubmitEditing fire’s off an a TextInput.

onSubmitEditing={ () => { this.hideKeyboard() /* <- the code above */ } } 
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found