🪴 notes

Search

Search IconIcon to open search

call createApi endpoints outside react components

Published May 5, 2023 Last updated May 5, 2023 Edit Source

rtk-q createApi endpoints can be called from outside react components by dispatching the returned redux thunk from the endpoints’ .initiate().

Example from react forms.

1
const res = await store.dispatch(authApi.endpoints.forgotPassword.initiate(userInput.email));

The redux store instance needs to be imported to .dispatch() the endpoint. The response data will be added to the store and cached. More on rtk query’s cache behavior here.