我正在学习Redux,我有两个reducer,一个contactReducer用于在页面上显示联系人,还有一个testReducer可以用来摆弄。在我的一个组件文件中,我具有此功能:
const mapDispatchToProps = (dispatch) => ({
getContacts: () => dispatch({ type: "TEST_ACTION" }),
});
这是我的两个减速器文件:contactReducer:
import { GET_CONTACTS } from "../actions/types";
const initialState = {
contacts: [
{
id: 1,
name: "John Doe",
email: "[email protected]",
phone: "555-555-5555",
},
{
id: 2,
name: "Karen Williams",
email: "[email protected]",
phone: "444-444-4444",
},
{
id: 3,
name: "Henry Johnson",
email: "[email protected]",
phone: "333-333-333",
},
],
};
export default function (state = initialState, action) {
switch (action.type) {
case GET_CONTACTS:
return {
...state,
};
default:
console.log("testing action in contactReducer");
return state;
}
}
和testReducer:
import { GET_CONTACTS } from "../actions/types";
const initialState = {
contactsTest: [
{
id: 1,
name: "ffffffffffff",
email: "[email protected]",
phone: "555-555-5555",
},
{
id: 2,
name: "ggggggggggggg",
email: "[email protected]",
phone: "444-444-4444",
},
{
id: 3,
name: "aaaaaaaaaaaaaa",
email: "[email protected]",
phone: "333-333-333",
},
],
};
export default function (state = initialState, action) {
switch (action.type) {
case "TEST_ACTION":
return {
...state,
};
default:
console.log("testing action");
return state;
}
}
因此,我在reducer文件中的console.log语句中注意到,对于每个联系人,contactReducer和testReducer的函数都在此行中被调用:
getContacts: () => dispatch({ type: "TEST_ACTION" }),
});
如果我有多个化简器,但我只想调用它们的一个函数进行调度,那我该怎么办?
combineReducers
,是redux中的帮助函数,可帮助您划分减速器。看一下这个链接:LINK