gsy
gsy

Reputation: 387

React Native - How to prevent the modal from closing when user hit the ESC key

When a user attaches a keyboard to a react-native app, and presses the ESC key, it will hide the modal component. The component is not unmounted so it virtually freezes the entire app.

How do you trap or prevent ESC key from closing/hiding the modal?

Something like this

import { Modal } from 'react-native';

<Modal
    onKeyPress={(e) => e.nativeEvent.key == 27 && e.nativeEvent.preventDefault() or e.preventDefault()?

TIA!

Upvotes: 0

Views: 1962

Answers (1)

misatrincado
misatrincado

Reputation: 51

The way to get the key selected is with 'onKeyPress'

 onKeyPress={(e) => console.log(e.nativeEvent.key)}

and this return the name, for example: 'Enter' or 'Backspace'. please search the name exactly of the Esc key and create the condition with that.

Upvotes: 1

Related Questions