Search code examples
reactjsreduxredux-toolkitrtk-query

Using RTK query how do I add data to my state without a call to the server?


I'm building my first app using RTK query. I need to set my authorization token in my headers. It is stored in a cookie, but they can't be accessed from the apiSlice, which means I need to store the token in my states which I'd have access to when setting my headers.

I get the token from my cookies when the app first starts, but I'm failing to understand how do I store it in my state, now that I use RTK query? I'm not making any API call, I already have the data. How do I store it in my slice?


Solution

  • The point of RTK Query is to make calls to the server and store the result of that call to the server.

    RTK Query does not replace all your other state management - it just takes over that one aspect. If you have non-server-data, you should still be using a normal slice for that, not try to put that into RTK Query.

    Apart from that: if you have a token that is in a cookie, there is probably no need to read that into your store and attach it to future requests. The point of cookies is that they are sent back and forth between the browser and the server - you don't have to manually do anything here, besides setting the credentials: "include" option.