简体   繁体   English

从redux减速器获取结果

[英]Getting results back from redux reducers

I have a function that is called from external code. 我有一个从外部代码调用的函数。 Its effect should be to create an object in my redux store based on passed data, and to return the object's id as a handle for future updates: 它的作用应该是根据传递的数据在我的redux存储中创建一个对象,并返回该对象的id作为未来更新的句柄:

const addNode = (data) => {
    dispatch(createNode(data));
    ...
    return createdNodeId;
}

My problem is: how to get the id of the object that was created as a result of the dispatched action? 我的问题是:如何获取由于调度操作而创建的对象的id?

The dispatch returns the full state, not just the new object. 调度返回完整状态,而不仅仅是新对象。 I see a couple of options, but I'm unsure which one would be best... 我看到了几个选项,但我不确定哪一个最好......

Option 1: Generate and pass ID 选项1:生成并传递ID

Generate the id myself in addNode and pass it into createNode(...) with the rest of the data. 在addNode中自己生成id,并将其与其余数据一起传递给createNode(...)。

That's probably the easiest from a practical point of view, but I find it horrible from a conceptual standpoint. 从实际的角度来看,这可能是最简单的,但从概念的角度来看,我觉得这很糟糕。 The formerly internal id generation strategy is suddenly exposed in my function, and what's more, depending on the id strategy, I might have to deal with all kinds of id clashes and/or synchronization to avoid those. 之前的内部id生成策略突然暴露在我的函数中,而且根据id策略,我可能必须处理各种id冲突和/或同步以避免这些。

Option 2: Extract ID from result state 选项2:从结果状态中提取ID

I guess it would be possible to extrapolate the new node from the result state. 我想可以从结果状态推断出新节点。 But that can get very complicated very quickly, never mind slow... 但是这很快就会变得非常复杂,更别慢了......

Option 3: Pass ID back in action 选项3:将ID传回实际操作

Actions are supposed to be immutable. 动作应该是不可变的。 But what would actually happen if I would designate a "result" property in my action that the reducer would write to to pass out the desired information? 但是,如果我在我的行动中指定一个“结果”属性,减速器会写入以传递所需信息,实际会发生什么? In my understanding this should work fine, as long as the "result" property is never treated as part of the action's input payload by the reducer. 在我的理解中,这应该可以正常工作,只要“result”属性永远不会被reducer视为动作输入有效负载的一部分。

This is still a bit dodgy, since the action is now partially mutable. 这仍然有点狡猾,因为行动现在部分可变。 But given the other options, it's my favorite right now. 但鉴于其他选择,它现在是我的最爱。

Edit: A similar question was asked before, but the answer was to change the reducer to avoid the need of the result. 编辑:之前曾问过类似的问题 ,但答案是更改减速器以避免需要结果。 Sadly, that's not an option here. 可悲的是,这不是一个选择。

It sounds like it's currently your reducer that is generating the id for the newly created object. 听起来它是当前你的reducer,它为新创建的对象生成id。 If that' correct, then I'm guessing its a fairly simple, synchronous process. 如果这是正确的,那么我猜它是一个相当简单的同步过程。 It also doesn't seem right to me. 这对我来说似乎也不对。 I would expect the reducer to only act on the data passed in the action, which should include the new object's id. 我希望reducer 对动作中传递的数据起作用,其中应包括新对象的id。 So I would suggest option 4.. 所以我建议选项4 ..

Break out the code that generates the id from the reducer into it's own space. 将从reducer生成id的代码分解到它自己的空间。 Use that code to include the id in the action dispatched to create the new object, either from any code that dispatches the action, or from the action creator itself. 使用该代码将id包含在分派的操作中,以便从调度操作的任何代码或从操作创建者本身创建新对象。 Return the id from your function. 从函数中返回id。

const addNode = (data) => {
  const action = createNode(data);
  dispatch(action);
  ...
  return action.payload.id;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM