ammarahm-ed / react-native-actions-sheet

A Cross Platform(Android, iOS & Web) ActionSheet with a flexible api, native performance and zero dependency code for react native. Create anything you want inside ActionSheet.
https://rnas.vercel.app
MIT License
1.49k stars 121 forks source link

[0.8.19] payload is destroyed too early when closing the sheet? #269

Closed efstathiosntonas closed 1 year ago

efstathiosntonas commented 1 year ago

Sorry for the bad issue title.

After upgrading to 0.8.19 (and prior versions down to 0.8.13 which is working fine) every time I close the sheet everything that is attached to the payload is undefined. Don't know if something has changed in the props in the latest versions (since there's no changelog).

Here's the payload I'm using:

const BookmarkedPostActionSheet = (props: {
  payload: { bookmark: Post_Bookmark; navigation: any };
  sheetId: string;
}) => {
}

The moment I close the sheet bookmark and navigation are undefined.

sheets.tsx:

import { registerSheet } from "react-native-actions-sheet";

import BookmarkedPostActionSheet from "@components/ActionSheets/BookmarkedPostActionSheet";

registerSheet("bookmark_action_sheet", BookmarkedPostActionSheet);

export {};
ammarahm-ed commented 1 year ago

@efstathiosntonas Try 0.8.20 and see if it works for you?

efstathiosntonas commented 1 year ago

@ammarahm-ed the same issue. I've console.log(props):

----------APP OPENS------
[] global
-------- SHEET OPENS------
 LOG  {"payload": {"bookmark": {"__typename": "post_bookmark", "id": "20f9e30c-983b-4b18-9219-9cd6e1a1deea", "post": [Object]}, "navigation": {"addListener": [Function addListener], "canGoBack": [Function canGoBack], "dispatch": [Function dispatch], "getId": [Function getId], "getParent": [Function getParent], "getState": [Function anonymous], "goBack": [Function anonymous], "isFocused": [Function isFocused], "navigate": [Function anonymous], "pop": [Function anonymous], "popToTop": [Function anonymous], "push": [Function anonymous], "removeListener": [Function removeListener], "replace": [Function anonymous], "reset": [Function anonymous], "setOptions": [Function setOptions], "setParams": [Function anonymous]}}, "sheetId": "bookmark_action_sheet"}

 -------------HERE I CLOSED THE SHEET------------------------
 LOG  ["global"] $$-auto-bookmark_action_sheet-global-provider
 LOG  {"payload": undefined, "sheetId": "bookmark_action_sheet"}
 LOG  {"payload": undefined, "sheetId": "bookmark_action_sheet"}
 ERROR  TypeError: Cannot read property 'bookmark' of undefined
ammarahm-ed commented 1 year ago

@efstathiosntonas How are you setting the payload?

efstathiosntonas commented 1 year ago

I call it like this:

  const onOpenPostMainSheet = useCallback(async () => {
    await SheetManager.show("bookmark_action_sheet", {
      payload: { bookmark, navigation }
    });
  }, [bookmark, navigation]);
ammarahm-ed commented 1 year ago

@efstathiosntonas That's opening the sheet, how are you closing it/setting payload?

efstathiosntonas commented 1 year ago

That's how I'm closing it, note that this works fine on 0.8.13

  await SheetManager.hide("bookmark_action_sheet");
ammarahm-ed commented 1 year ago

And so payload inside the sheet is undefined? Let me see.

efstathiosntonas commented 1 year ago

Yes, when it opens the payload is there just fine, the moment I close it it breaks. Maybe there's a race condition and props are cleaned up too early.

ammarahm-ed commented 1 year ago

@efstathiosntonas Why do you need props inside the sheet after it is closed?

efstathiosntonas commented 1 year ago

I do not need them any more. They're part of a useCallback dependencies array:

  const onRemovePostBookmark = useCallback(async () => {
    await SheetManager.hide("bookmark_action_sheet");
    await removePostBookmark({
      variables: {
        bookmark_id: props.payload.bookmark.id
      },
      update(cache) {
        cache.evict({
          id: cache.identify({
            id: props.payload.bookmark.id,
            __typename: "post_bookmark"
          })
        });
        cache.gc();
      }
    });
    track("User Removed Bookmark From a Post", {
      post_id: props.payload.bookmark.post.id,
      profile_id: currentUser().uid
    }).catch();
  }, [
    props.payload.bookmark.id,
    props.payload.bookmark.post.id,
    removePostBookmark,
    track
  ]);

We need to treat them as optional from now on? 🤔 If yes, then I'll have to go through 30 action sheets 🥴

ammarahm-ed commented 1 year ago

Okay, I will try to repro this locally, not sure what I changed that is causing this.

efstathiosntonas commented 1 year ago

Thanks @ammarahm-ed, keep it up! I ll stick to 0.8.13 for now

ammarahm-ed commented 1 year ago

@efstathiosntonas Try 0.8.21. The problem is using props.payload.bookmark.id instead of simply props.payload but now it should work.

efstathiosntonas commented 1 year ago

@ammarahm-ed works as charm. Thank you brother.