Skip to content

RTK Query: How can I select the result after mutation from the store when I have two components? #1458

@GusevDV

Description

@GusevDV

Hello!
Recently I started using RTK Query and I have a problem with selecting data from the store after mutation. I don't understand what is the best way to do this.
For example, I have two components. I trigger mutation in the first component and I need to get results in the second component. I can't use the useMutation hook because it works fine only in one component. Also I don't want to put a useMutation hook to a parent.

My createAPI:

export const subscriptionApi = createApi({
  reducerPath: 'subscribe/create',
  baseQuery: fetchBaseQuery({ baseUrl: '/api' }),
  tagTypes: ['Subscription'],
  endpoints: (builder) => ({
    getSubscriptionStatus: builder.query<Subscription, string>({
      query: (serviceID: string) => `subscription/${serviceID}`,
    }),
    createSubscription: builder.mutation<Subscription, string>({
      query: (serviceID: string) => ({
        url: 'subscription/',
        method: 'POST',
        body: {
          serviceID,
        },
      }),
    }),
  }),
});

export const { useGetSubscriptionStatusQuery, useCreateSubscriptionMutation } = subscriptionApi;

My plan is to select the result from the store manually, but I don't know how I can do this the best way, because each request has a unique cache key. Please help me find any good solutions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions