DEV Community

Agik Setiawan
Agik Setiawan

Posted on

5 4

Put data from RTK Query to State Slice in Redux Toolkit

In CreateApi for RTK Query

export const authApi = createApi({
    reducerPath: 'authApi',
    baseQuery: axiosBaseQuery({
        baseUrl: environment.base_url
    }),
    endpoints: (builder) => ({
        user: builder.query<any,void>({
            query: () => ({
                url: API.GET_USER_URL,
                method: 'GET'
            }),
            transformResponse: (response: any) => response.data ?? []
        })
    })
})

export const { useUserQuery } = authApi;
export default authApi;
Enter fullscreen mode Exit fullscreen mode

In CreateSlice

Inside extrareducers

 builder.addMatcher(authApi.endpoints.user.matchFulfilled, (state, { payload }) => {
      state.user = payload;
    })
Enter fullscreen mode Exit fullscreen mode

we can put data from RTK Query into user state via addMacher

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay