0

i can't read array list of objects from database that this created by mongodb and i have problem reducer is:

export default (state = [], action) => {
    switch (action.type) {
        case 'INCREMENT_COUNTER':
            const counters = [...state];
            const index = counters.indexOf(action.counter);
            counters[index] = { ...action.counter };
            counters[index].value++;
            return [...counters];

        case 'DECREMENT_COUNTER':
            const counterss = [...state];
            const indexx = counterss.indexOf(action.counter);
            counterss[indexx] = { ...action.counter };
            counterss[indexx].value--;
            return [...counterss];

        case 'DELETE_COUNTER':
            const filteredCounters = state.filter(
                c => c.id !== action.counterId
            );
            return [...filteredCounters];

        case 'RESET_COUNTERS':
            const resetCounters = state.map(c => {
                c.value = 0;
                return c;
            });
            return [...resetCounters];
        default:
            return state;
    }
};

configure State:

import { createStore } from 'redux';
import counterReducer from '../reducers/counterReducer'

export default (defaultState) => {
    const store = createStore(counterReducer,defaultState);
    return store;
}

My initial state (defaultState) is not work all is true by hardcode in defaultState but not working by this

import { getItems } from '../services/itemService';

const defaultState = [getItems()];

export default defaultState;
mahmood_kd
  • 11
  • 1
  • 5
  • I would guess getItems returns a promise but you're treating it as if it's an array counters. – HMR Oct 22 '19 at 16:30
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – HMR Oct 22 '19 at 16:36

0 Answers0