我要向我的减速器发送很多东西
export const setStudent = (data, value, year, group, showStudent) => ({
type: "SET_STUDENT",
data,
value,
year,
group,
showStudent
});
在我的化简器中,我将需要排序的数据以及相关参数发送给它:
case "SET_STUDENT":
let studentName = data
.find(option => {
return option.id == year;
})
.options.find(option => {
return option.id == group;
})
.options.find(option => {
return option.id == value;
}).label;
return { ...state, student: value, label: studentName };
我可以将其导入data
中,而不是发送reducer
,就像这样:
import { data } from "../config/calendars.js";
我的mapDispatchToProps
也变得非常混乱:
const mapDispatchToProps = dispatch => ({
fetchEvents: id => dispatch(fetchEvents(id)),
isLoadingCredentials: loadingCredentials =>
dispatch(isLoadingCredentials(loadingCredentials)),
setCredentials: credentials => dispatch(setCredentials(credentials)),
setYear: year => dispatch(setYear(year)),
setGroup: (group, data, year) =>
dispatch(setGroup(group, data, year)),
setStudent: (data, e, year, group, showStudent) =>
dispatch(setStudent(data, e, year, group, showStudent)),
resetForm: () => dispatch(resetForm()),
setAuthenticated: value => dispatch(setAuthenticated(value))
});
答案 0 :(得分:1)
您可以使用"object shorthand" form of mapDispatch
来大大简化代码。只需使用动作创建者定义一个对象,例如:
const mapDispatchToProps = {
fetchEvents,
isLoadingCredentials,
setCredentials,
setYear,
setGroup,
setStudent,
resetForm,
setAuthenticated,
}
此外,我猜您可能可以将其中一些“设置”操作合并为单个"USER_FORM_UPDATED"
操作或类似操作。
答案 1 :(得分:0)
如果数据是静态对象,则可以从reducer导入它。它将保持纯功能。但是我会尽可能简化我的化简器,也许我会将此逻辑移至动作创建者。这就是为什么我们首先要创建动作创建者,使他们也可以包含逻辑的原因。
例如:
import { data } from "../config/calendars.js";
export const setStudent = (value, year, group, showStudent) => {
let studentName = data
.find(option => {
return option.id == year;
})
.options.find(option => {
return option.id == group;
})
.options.find(option => {
return option.id == value;
}).label;
return {
type: "SET_STUDENT",
student: value,
label: studentName
};
}