我确实使用了 combineReducers(),并且在每个reducer中分别定义了 initialState 。
问题是:
->状态似乎总是不确定的,即使在化简器中,我也尝试console.log({...state})
并得到一个空对象。
在每次返回{}中将 state 替换为 initialState 时,我会获得initialState数据。
我在 clientsReducer 函数中尝试了以下操作:
1- state = {...initialState}
=>给出一个空对象
2-从参数中删除initialState
并在函数内部进行分配。 =>给出未定义
知道为什么吗?
const initialState = {
clientsArray: [],
shouldFetchClients: false,
isFetchingClients: false,
fetchClientsError: null,
};
export default function clientsReducer(state = initialState, action) {
switch (action.type) {
case FETCH_CLIENTS_SUCCESS:
return {
...state,
clientsArray: action.payload,
isFetchingClients: false,
};
case FETCH_CLIENTS_FAILURE:
return {
...state,
fetchClientsError: action.payload,
isFetchingClients: false,
};
case FETCH_CLIENTS:
return {
...state,
isFetchingClients: true,
shouldFetchClients: true,
clientsArray: null,
};
default:
return state;
}
}
这是化简器的代码,我正在使用CombineReducers函数定义RootReducer:
import { combineReducers } from 'redux';
//REDUCER FILES IMPORTS HERE
export default combineReducers({
clients: clientsReducer,
admins: adminsReducer
})
然后,在createStore中,我有:
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import { persistReducer, persistStore } from 'redux-persist';
import createSagaMiddleware from 'redux-saga';
import storage from 'redux-persist/lib/storage';
import createFilter from 'redux-persist-transform-filter';
// CONNECTED-REACT-ROUTER
import { createBrowserHistory } from 'history';
import { routerMiddleware, connectRouter } from 'connected-react-router';
import rootReducer from './rootReducer';
export const history = createBrowserHistory();
const sagaMiddleware = createSagaMiddleware();
const persistedReducer = persistReducer(persistConfig, rootReducer);
export const store = createStore(
connectRouter(history)(persistedReducer),
composeEnhancer(
applyMiddleware(
routerMiddleware(history),
sagaMiddleware,
thunk,
createLogService(true),
),
),
);