reactjs - 在 mapDispatchToProps 中使用 dispatch 时使用哪个 reducer?
问题描述
我正在学习 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: "john@gmail.com",
phone: "555-555-5555",
},
{
id: 2,
name: "Karen Williams",
email: "karen@gmail.com",
phone: "444-444-4444",
},
{
id: 3,
name: "Henry Johnson",
email: "henry@gmail.com",
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: "john@gmail.com",
phone: "555-555-5555",
},
{
id: 2,
name: "ggggggggggggg",
email: "karen@gmail.com",
phone: "444-444-4444",
},
{
id: 3,
name: "aaaaaaaaaaaaaa",
email: "henry@gmail.com",
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" }),
});
如果我有多个 reducer,但我只想调用它们的一个函数进行调度,我会怎么做?
解决方案
combineReducers
, 是redux中一个帮助你划分reducer的辅助函数。看看这个链接:LINK
推荐阅读
- android - Android如何将线性布局的高度设置为listview元素?
- java - Java:如何查找对特定类实例的所有引用
- apache-kafka - 在 Java 中创建 kafka 主题
- android - Xamarin Android Material Calendar View 今天更新选择
- ios - 将作为 textfield 的 rightView 添加的 UIImageView 与 skyfloatinglabeltextfield 的文本垂直对齐
- c# - 在执行自动化脚本期间,Web 应用程序的响应时间会增加
- java - 如何在 Java 中动态初始化数组?
- c# - 为什么发生异常时未从 Mvc 设置 StatusCode
- python - 在python中为groupby动态选择数据框列
- java - 需要 POJO 对象才能使用 Spring Boot 自动装配以下 yml 结构