Closed kyawkhantwin closed 6 months ago
This is injectEndpoints File
import { createEntityAdapter, createSelector } from "@reduxjs/toolkit"; import apiSlice from "../api/apiSlice"; const productsAdapter = createEntityAdapter({}) const initialState = productsAdapter.getInitialState(); export const extendedApiSlice = apiSlice.injectEndpoints({ endpoints: (builder) => ({ getProducts: builder.query({ query: () => "/todos", transformResponse: (responseData) => { return productsAdapter.setAll(initialState, responseData); }, }) }), }); export const { useGetProductQuery } = extendedApiSlice; const selectProductsResult = extendedApiSlice.endpoints.getProducts.select(); const selectProductsData = createSelector( selectProductsResult, (productsResult) => productsResult.data ); export const { selectAll: selectAllProducts, selectById: selectProductById, selectIds: selectProductIds, } = productsAdapter.getSelectors( (state) => selectProductsData(state) ?? initialState );
This is apiSlice
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react"; export const apiSlice = createApi({ reducerPath: 'api', // optional baseQuery: fetchBaseQuery({ baseUrl: 'https://jsonplaceholder.typicode.com' }), tagTypes: ['Product', 'Category'], endpoints: builder => ({}) }) export default apiSlice;
This is injectEndpoints File
This is apiSlice