0

I am storing data in Redux store but instead of updating the store property its creating nested duplicates

enter image description here

Index.js

const initialStore = {
    user: {},
    brands: [],
    category: []
}
ReactDOM.render(
    <Provider store={configureStore(initialStore)}>
        <App />
    </Provider>,
    document.getElementById('root')
);

Store.js

import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers/rootReducer';
const enhancers = [];
const middleware = [
    thunk
];

const windowIfDefined = typeof window === 'undefined' ? null : window;
if (windowIfDefined && windowIfDefined.__REDUX_DEVTOOLS_EXTENSION__) {
    enhancers.push(windowIfDefined.__REDUX_DEVTOOLS_EXTENSION__());
}

export default function configureStore(initialState = {}) {
    return createStore(
        rootReducer,
        initialState,
        compose(applyMiddleware(...middleware), ...enhancers)
    );
}

RootReducer.js

import { combineReducers } from 'redux';
import brandsReducer from './brandsReducer';
import userReducer from "./userReducer";
import categoryReducer from "./categoryReducer";
export default combineReducers({
    brands: brandsReducer,
    user: userReducer,
    category: categoryReducer
});

CategoryReducer.js

export default (state = {}, action) => {
    switch (action.type) {
        case 'UPDATE_CATEGORIES':
            return Object.assign({}, state, { category: action.payload });
        case 'TOGGLE_CATEGORY_SELECTION':
            return {
                ...state, category: { ...state.category, category: action.payload }
            }
        default:
            return state;
    }
}

I want to store in store-> category -> Array & store-> brands -> Array in this format.

2
  • 1
    Is the category state inside CategoryReducer an array? If so then shouldn't you be using [] instead of {} while destructuring and appending category? Commented May 16, 2021 at 5:35
  • Yes @MohammadAbdulAlim category is an array. Okay ll check Commented May 16, 2021 at 5:55

1 Answer 1

1

in CategoryReducer.js have the case 'TOGGLE_CATEGORY_SELECTION' look like this

return { ...state, category: [ ...state.category, ...action.payload ] }

or if you do not want to add to values to category array in the store

return { ...state, category: action.payload }
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Kevin TOGGLE_CATEGORY_SELECTION this action is not triggering only UPDATE_CATEGORIES is triggering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.