Can I access POST mutation response from another component? That is, is it cached like a Query/GET request?
API with POST/mutation
createTestto server:Of note, I do not have a GET request endpoint to GET Tests, unfortunately.
export const testApi = createApi({ reducerPath: 'testApi', baseQuery: fetchBaseQuery({ baseUrl: TEST_API_BASE_PATH }), tagTypes: ['Test'], endpoints: (builder) => ({ createTest: builder.mutation< CreateTestResponse, CreateTestRequest >({ query: (payload) => ({ url: '', method: 'POST', body: payload, }), invalidatesTags: ['Test'], }), }), }); export const { useCreateTestMutation } = testApi;Payload is an object retrieved from a form component.
POST returns a response.
For example:
{ data: { firstName: "Bluey", lastName: "Heeler" } }I call
createTestin a component like this:const SomeComponent = ({ payload }) => { const [createTest] = useCreateTestMutation(); const someCallback = useCallback(() => { try { const testResult = createTest(payload); // do stuff with Test Result } catch (err) { // do stuff with error } }, [...]); return ( <div> ... <Button onClick={someCallback} /> .... </div> ) }I would like to get the result/response of that mutation in another component, similar to the cached response from a GET request/useQuery. Do you know how?
I have resorted to creating an RTK Slice and setting the response there in global state. I get the feeling that that is not the "correct" way of using RTK Query, however.