如何从我的数据库(MongoDB)中获取初始状态以使用Redux

时间:2019-10-22 16:27:26

标签: reactjs redux

我无法从mongodb创建的数据库中读取对象的数组列表 我有问题 减速器是:

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;
    }
};

配置状态:

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

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

我的初始状态(defaultState)不起作用 通过defaultState中的硬编码,所有内容都是正确的,但不能通过

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

const defaultState = [getItems()];

export default defaultState;

0 个答案:

没有答案