Bro*_*mox 5 javascript reducers reactjs redux
在我的应用程序中,我有分配作业的学生。每次创建学生时,我都会循环遍历当前作业并更新每个学生的作业数组。
我的问题是当我创建作业时。不仅需要创建该作业,而且创建后我还需要将其分配给特定的学生。
我已将我的应用程序分成三个减速器;班级、学生和作业。
如何从作业缩减器更新我的学生状态?我需要等到作业创建完毕,然后将作业数组分配给每个学生。也许我可以发送一个操作,然后发送另一个操作?
case actionTypes.CREATEHOMEWORKS:
// Get new homework description
const newHomework = action.newHomework
// Get the currently active class
const activeClass = action.activeClass.key;
// Get users current browser data
const dateCreated = getCurrentDate();
// Generare a new UID
const key = guid();
// Create a new homework object
const homework = { key: key, class: activeClass, dateCreated: dateCreated, description: newHomework };
// Get the current homework array
const homeworks = JSON.parse(JSON.stringify(state.homeworks));
// Combine current homework with new homework object.
homeworks.push(homework);
// I now need to update students to have this homework but they are handled
// in another state
Run Code Online (Sandbox Code Playgroud)
你的减速器应该是一个纯函数 - 也就是说,给定相同的输入,它将始终生成相同的输出。您正在减速器中生成随机 GUID,因此相同的输入永远不会给出相同的输出。如果您要将 GUID 生成移至操作中,则可以将其传递到CREATEHOMEWORK操作的有效负载中,然后将其分派到ASSIGNHOMEWORK您的students减速器处理的操作中。