DEV Community

Agik Setiawan
Agik Setiawan

Posted on

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)