snesjhon / mediaDiary

https://mediadiary.app
1 stars 0 forks source link

[bug] state.isSaving & SWR #182

Open snesjhon opened 3 years ago

snesjhon commented 3 years ago

Problem

 useEffect(() => {
    if (state.isSaving) {
      mutate();
    }
  }, [state.isSaving, mutate]);

I'm using this to trigger a mutate for the current key model.

Since i'm doing this

 const { data, error, size, setSize, mutate } = useSWRInfinite<
    MediaDiaryWithId[]
  >(
    (_, prev) => {
      // We've reached the end of the list since we got < 30, don't call again
      if (prev && prev.length < 30) return null;

      return [
        "/fuego/diary",
        user.uid,
        prev !== null ? prev[prev.length - 1].addedDate : null,
        state.bookmarkFilters?.mediaType ?? null,
        state.bookmarkFilters?.releasedDecade ?? null,
        state.bookmarkFilters?.addedDate ?? null,
        state.bookmarkFilters?.genre ?? null,
      ];
    },
    fuegoBookmarkGet,
    {
      revalidateOnFocus: false,
    }
  );

I can't mutate based on key, so there's a lag when I click and mutate. I'm assuming some kind of race condition maybe. Not sure. But there is something is happening between those states that aren't allowing for proper instant mutation of the keys

snesjhon commented 3 years ago

I think it might be necessary to add a Context to subscribe to the mutate of the "current" available "view"