redux-toolkit icon indicating copy to clipboard operation
redux-toolkit copied to clipboard

Help - How to memoize RTKQ selector with dynamic argument from state, not selector arg.

Open padge opened this issue 10 months ago • 8 comments

Looking for help on this, as I can't wrap my head around how to get a proper memoized selector using RTKQ... The selector needs the gameId from store state, not a selector arg.

This is what I have right now that isn't memoizing:

const createInventorySelector = createSelector(
  (id: string) => id,
  (id) => api.endpoints.listInventoryForGame.select(id),
);
export const selectInventory = createSelector(
  (state: RootState) => state,
  selectGameId,
  (state: RootState, gameId) => createInventorySelector(gameId)(state),
);

const inventory = selectInventory(state);

I know state => state isn't good, but I'm trying to follow the example here https://redux-toolkit.js.org/rtk-query/usage/usage-without-react-hooks#memoization, and don't see how to do it otherwise.

Thanks.

padge avatar Apr 22 '24 16:04 padge