K
K
kirillleogky2020-11-30 11:58:33
redux
kirillleogky, 2020-11-30 11:58:33

Why does a reducer throw a ReferenceError?

import { createReducer } from '../storeUtils';
import { SET_PRODUCT_DEPARTMENTS } from '../actions/productActions';

const initialState = {
  productDepartments: []
};

function setProductDepartments(state, action) {
  return {
    ...state,
    productDepartments: action.payload
  };
}

export default createReducer(initialState, {
  [SET_PRODUCT_DEPARTMENTS]: setProductDepartments
});


In this reducer, when you enter the "next build" command, an error appears:
ReferenceError: Cannot access 'SET_PRODUCT_DEPARTMENTS' before initialization


And in this case, everything is gut:
import { SET_PRODUCT_DEPARTMENTS } from '../actions/productActions';

const initialState = {
  productDepartments: []
};

const productReducer = (state = initialState, action) => {

    switch (action.type) {
        case SET_PRODUCT_DEPARTMENTS:
            return {
                ...state,
                productDepartments: action.payload
            };

        default:
            return state;
    }
};

export default productReducer;


Tell me what was the problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question