1

i am learning react js by doing a project wher i am using RTK to manipulate the state of my app . The problem is after fetching data i want my store to change automatically . this is my server

import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

export const openSeaApi = createApi({
  reducerPath: "collectionsApi",
  baseQuery: fetchBaseQuery({
  baseUrl: `https://testnets-api.opensea.io/api/v1`,
}),

 endpoints: (builder) => ({
 retreiveCollections: builder.query({
  query: ({ asset_owner, offset, limit }) =>
    `collections?asset_owner=${asset_owner}&offset=${offset}&limit=${limit}`,
 }),
 }),
 });

  export const { useRetreiveCollectionsQuery } = openSeaApi;

This is my slice file:

import { createSlice } from "@reduxjs/toolkit";

const initialState = {
 allCollections: [],
 isError: false,
 isSuccess: false,
 isLoading: false,
  message: "",
};

  export const collectionsSlice = createSlice({
    name: "collection",
    initialState,
reducers: {
  appendCollections: (state, action) => {
   state.allCollections = { ...state.allCollections, ...action.payload };
  },
 },
});

 export const { appendCollections } = collectionsSlice.actions;
 export default collectionsSlice.reducer;

this my store.js

import { configureStore } from "@reduxjs/toolkit";
import { setupListeners } from "@reduxjs/toolkit/dist/query";
import collectionReducer from "../features/collections/collectionSlice";
import { openSeaApi } from "../services/openseaService";

export const store = configureStore({

reducer: {
collection: collectionReducer,
[openSeaApi.reducerPath]: openSeaApi.reducer,
 },


  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware().concat(openSeaApi.middleware),
   });
       setupListeners(store.dispatch);

and my component

  const user = JSON.parse(localStorage.getItem("user"));

   const { data, isError, isLoading } = useRetreiveCollectionsQuery({
   asset_owner: user?.account,
    offset: 0,
    limit: 300,
 });

 

So when i look in my store i fiend the collection reducer but it doesn't get the data even after the action is fulfilled . how can i put the data into the collections array in the store . thank you .

Ghost
  • 318
  • 2
  • 8

1 Answers1

2

That data is already in the store. The point is that RTK Query manages the data for you - you don't have to write a slice by hand any more. Just use the hook in your component and you're done.

phry
  • 35,762
  • 5
  • 67
  • 81