更新:我正在使用 React-Boilerplate,除了容器/组件之外,未对原始版本进行修改。当第一次调度新操作时,减速器会运行多次,有时会运行两次以上,但随后调度相同的操作时则不会。重复的减速器调用不会触发操作本身,但状态会更新并重新呈现组件。
例如:如果我调度更新reducerCase1的action1,但不调度更新reducerCase2的action2,则action1将运行一次,而reducerCase1将运行两次。 action2和reducerCase2将不会运行。如果我然后调度更新reducerCase3的action3,则reducerCase1将被多次调用,但action1、action2和reducerCase2将不会被调用。
如果我以与action1相同的方式调度action,它将被以与action1和reducerCase1相同的方式处理,多次运行reducer而不触发该action。
如果在这之后我第二次调度 action3,reducerCase1 将根本不会运行(应该是这种情况)。
在这里,我对 GET_CATEGORIES 和 GET_CATS_COMPLETED 操作感兴趣:
这是减速器内的控制台日志:
export default function Categories(state = initialState, action) {
switch (action.type) {
case GET_CATEGORIES:
debugger;
console.log('getting categories...');
return state.set('isLoading', true);
case GET_CATEGORIES_COMPLETED:
debugger;
console.log('setting categories...');
return state
.set('categories', fromJS(action.cats))
.set('isLoading', false);
由于我的所有减速器都发生这种情况,我认为它与 mapDispatchToProps 有关:
const mapStateToProps = createStructuredSelector({
categories: makeSelectCategories(),
ownerId: makeSelectProfileId(),
selectedCategoryId: makeSelectSelectedCategoryId(),
isLoading: makeSelectIsLoading(),
});
const mapDispatchToProps = {
getCategories,
setCategory,
};
const withConnect = connect(
mapStateToProps,
mapDispatchToProps
);
const withReducer = injectReducer({ key: 'CategoryContainer', reducer });
const withSaga = injectSaga({ key: 'CategoryContainer', saga });
export default compose(
withSaga,
withReducer,
withConnect
)(CategoryContainer);
这是我的行动:
export function getCategories() {
console.log('inside getCategories()');
return {
type: GET_CATEGORIES,
};
}
export function getCategoriesCompleted(cats) {
return {
type: GET_CATEGORIES_COMPLETED,
cats,
};
}
最后是传奇:
export default function* CategoryContainerSaga() {
yield takeLatest(GET_CATEGORIES, getCategories);
}
function* getCategories() {
try {
const plidParam = yield call(getPlParam);
const profileId = yield call(getProfileId);
const url = getUrl();
const { categories2, playlist } = yield call(
getCatsRequest,
url,
profileId,
plidParam
);
yield put(getCategoriesCompleted(categories2));
if (playlist) yield put(setPlaylist(playlist));
} catch (error) {
yield put(getCategoriesCompleted([]));
yield put(setError(error.message));
}
}