expo / react-native-action-sheet

A cross-platform ActionSheet for React Native
MIT License
1.37k stars 224 forks source link

Allowing to await the callback result and return a result #287

Closed sebastienlabine closed 1 year ago

sebastienlabine commented 1 year ago

Could it be possible to allow us to await the result of the callback and return it?


export async function callback(index) {
    switch (index) {
        case 0:
            var response = await someAsyncFunction();
            return response;
        default:
            return undefined;
    } 
}
const response = await showActionSheetWithOptions(options, callback)
bradbyte commented 1 year ago

Possible, maybe, but I'm not sure that would be ideal. iOS wouldn't work with that implementation since ActionSheetIOS doesn't support that. Also, showActionSheetWithOptions doesn't return anything, so making it async wouldn't matter anyway.

Here's an idea using state...

const [response, setResponse] = useState(null);

useEffect(() => {
  if (response) {
    // something we care about with response
  }
}, [response]);

const callback = async (index) => {
  switch (index) {
    case 0:
      setResponse(await someAsyncFunction());
      break;

    default:
      setResponse();
  } 
}

const onPress = () => {
  showActionSheetWithOptions(options, callback);
}
sebastienlabine commented 1 year ago

Yes this is pretty much what i’m doing right now but I really don’t find it intuitive. My guess was that it’s natively not supported that’s why the option is not available.

My use case was about a reusable hook that lets pick images from multiple sources (camera, media library or document picker).

so I need to do


const [showPicker, {isLoading, assets}] = useMediaPicker();

useEffect(()=> {
if(assets) {}
},[assets]);

await showPicker();

Instead of only


var assets = await showPicker();
ˋ``