score:1

Accepted answer

looks like you are mixing up the api layer and storage. rtk-q brings you a wrapping layer with cache and other api-related features to make all the data-fetching fetch lifecycle easier and consistent all over the app.

what are you looking for - is a data storage-related question. it can be handled with any of the existing approaches like redux, context, localstorage, etc.

i would say that the most suitable solution for you here - is just to put userid to the localstorage\cookies once after logging in, and after - read it during all the requests in future calls.

to help you with that - you can define a custom queryfunction:

export const custombasequery: basequeryfn<string | fetchargs, unknown, fetchbasequeryerror> 
  = async (args, api, extraoptions = {}) => {
  const userid = localstorage.getitem('userid')
  // do what you want with userid, and pass it to the request

  const result = await fetchbasequery(args, api, extraoptions);

  // optionally - can handle the response in some way
  return result;
};

after that you can apply it to your api definition, instead of fetchbasequery:

basequery: custombasequery({ baseurl: location.origin }),

Related Query

More Query from same tag