我正在用Redux编写一个应用程序。当我执行一个操作时,它似乎取代了存储,只定义了在还原器中显式声明的值,而不是更新存储。
我正在使用来自handleActions包的redux-actions。减速机的例子是这样的;
import { handleActions } from 'redux-actions';
export default handleActions({
DOCUMENT_GET_ALL_PENDING: state => ({
isRefreshing: true,
}),
DOCUMENT_GET_ALL_FULFILLED: (state, action) => ({
document: action.payload.documents,
isRefreshing: false,
}),
}, { isRefreshing: false, documents: [] });例如,当它收到DOCUMENT_GET_ALL_PENDING时,文档数组会被设置为未定义的。下面是创建我的商店的样子;我使用的是redux-promise-middleware、redux-devtools-extension和thunk中间件。
const store = createStore(
rootReducer,
defaultState,
composeWithDevTools(applyMiddleware(promiseMiddleware(), thunk)),
);欢迎任何和所有的帮助或建议。
发布于 2018-04-21 13:08:05
您需要将您的状态与前一个状态合并。现在,您只是返回新的状态值。
有很多方法可以做到这一点,但是您可以使用es6 spread operator。
DOCUMENT_GET_ALL_PENDING: state => ({
...state,
isRefreshing: true,
}),
DOCUMENT_GET_ALL_FULFILLED: (state, action) => ({
...state,
document: action.payload.documents,
isRefreshing: false,
})https://stackoverflow.com/questions/49955963
复制相似问题