react-native-web
react-native-web copied to clipboard
Pressable triggers onPress twice, when activated via Enter key and set to disabled
Is there an existing issue for this?
- [X] I have searched the existing issues
Describe the issue
When using the onPress
prop on the Pressable
component and setting it to disabled
inside the onPress
-function, the function will be called a second time. But only when activating the button via the Enter key.
First time onPress
is called when pressing the Enter key down and the second time it is called when releasing the Enter key. I created a test case which demonstrates this behaviour.
Expected behavior
onPress
is only called once, like it is when using Space key oder pressing the button with the mouse.
Steps to reproduce
- Use
<Pressable role="button">
and apply anonPress
handler to it - Set a
disabled
state inside theonPress
handler - Set the
disabled
prop using the state on yourPressable
- Put some
console.log
inside theonPress
handler - Activate your button using the keyboard, pressing the Enter key
- See your
console.log
twice in the console
Test case
https://codesandbox.io/s/react-native-web-on-press-disabled-77crwk
Additional comments
No response
Is there a workaround for this till the fix is provided?
This is causing multiple form submissions in our application, resulting in duplicate database lines. Not ideal!
Because we never legitimately want to quickly submit a form twice in a row, I'm adding a throttle wrapper around the submit handler on our submit button
// This is a temporary fix until https://github.com/necolas/react-native-web/issues/2605 is fixed
// Prevent calling the submit function twice in quick succession (within 0.5 seconds). The first
// call completes immediately - it just blocks any subsequent calls within 0.5 seconds.
const throttle = <Callback extends (...args: any[]) => any>(callback: Callback) => {
let disabled = false;
return (...args: Parameters<Callback>): ReturnType<Callback> | undefined => {
if (disabled) {
return;
}
disabled = true;
setTimeout(() => {
disabled = false;
}, 500);
return callback(...args);
};
};
@necolas any idea when you're planning on a new release that includes this fix?
It's been out for a week already 54c14d64dabd175e8055e1dc92e9196c821f9b7d