# Redux Boilerplate

Store.js

```javascript
//store.js
import { configureStore } from "@reduxjs/toolkit";
import underlyingReducer from "../features/underlyings/underlyingSlice";
// import { createLogger } from 'redux-logger';

// const logger = createLogger({
//   collapsed: true,
//   predicate: (getState, action) => action.type !== 'socket/priceDataAdded'
// });

export const store = configureStore({
  reducer: {
    underlying: underlyingReducer,
  },
  // middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(logger)
});
```

Slice.js

```javascript
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";

const apiUrlBase = "www.google.com";

export const fetchListings = createAsyncThunk(
  "fetchDerivativeListings",
  async (token) => {
    let finalData = {
      payload: [],
    };
    const response = await fetch(`${apiUrlBase}/derivatives/${token}`);
    let stockData = await response.json();
    return finalData;
  },
);

const initialState = {
  isLoading: false,
  data: {
    stockData: [],
  },
  isError: false,
};

const derivativesSlice = createSlice({
  name: "derivatives",
  initialState,
  reducers: {
    priceDataAdded: (state, action) => {
      state.priceData[action.payload.token] = action.payload.price;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(fetchListings.pending, (state) => {
      state.isLoading = true;
      state.data.stockData = [];
    });
    builder.addCase(fetchListings.fulfilled, (state, action) => {
      state.isLoading = false;
      let { payload } = action;
      if (payload.success) {
        state.data.stockData = payload.payload;
        state.isError = false;
        state.data.message = "";
      } else {
        state.data.stockData = [];
        state.data.message = payload.err_msg;
        state.isError = true;
      }
    });
    builder.addCase(fetchListings.rejected, (state, action) => {
      console.log("Error ", action.payload);
    });
  },
});

export const { priceDataAdded } = derivativesSlice.actions;
export default derivativesSlice.reducer;
```

component.js

```javascript
import { useDispatch, useSelector } from "react-redux";

const state = useSelector((state) => state.underlying);
const socketData = useSelector((state) => state.socketData.priceData);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gautamnaik1994.gitbook.io/snippets/web-dev/redux-boilerplate.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
