expo / react-native-action-sheet

A cross-platform ActionSheet for React Native
MIT License
1.4k stars 226 forks source link

undefined in buttonIndex callback showActionSheetWithOptions #222

Closed pmgzo closed 3 years ago

pmgzo commented 3 years ago

Hi,

I would like to open this issue because I had some trouble with my app.

ActionSheet.showActionSheetWithOptions(
      {
        options,
      },
      (i) => {
        setSelectedKind(kinds[i]);
      }
    );

When my action sheet is displayed and I clicked on the return button of my emulator, the buttonIndex callback is triggered and I got parameter i as undefined that cause my app to crash. I thought that this parameter type was of type number: https://github.com/expo/react-native-action-sheet/blob/master/src/types.ts#L5

I also considered this pr: https://github.com/expo/react-native-action-sheet/pull/102, but I'm not sure to understand the point. Is it possible that this callback is not called if we click on the return button ? Are we obliged to put a cancel button on the action sheet to get the expected behavior ?

bradbyte commented 3 years ago

You should pass in a cancelButtonIndex or at least guard against an undefined i when calling setSelectedKind.

pmgzo commented 3 years ago

Thank you for you answer, I wanted to say that I received an undefined i in that callback which is not the expected type precised here: https://github.com/expo/react-native-action-sheet/blob/master/src/types.ts#L5. I propose to specify that same parameter to be either of type number or type undefined.

bradbyte commented 3 years ago

Thanks for the clarification!

bradbyte commented 3 years ago

:tada: This issue has been resolved in version 3.12.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

alamothe commented 3 years ago

In what cases can it be undefined? If cancelButtonIndex is passed, is it guaranteed it won't be? Thanks!

pmgzo commented 3 years ago

In what cases can it be undefined? If cancelButtonIndex is passed, is it guaranteed it won't be? Thanks!

What i wanted to say is just add the undefined type in typescript. Because the cancelButtonIndex is just acting at runtime. But yes, if you specify cancelButton it should work as expected !