简体   繁体   English

从 redux-observable 史诗中访问状态

[英]Accessing the state from within a redux-observable epic

I am using redux to build a little fusball-manager.我正在使用 redux 来构建一个小的 fusball-manager。 Basically it's a page that shows the score of both teams and some buttons to increment them.基本上它是一个页面,显示两支球队的得分和一些按钮来增加它们。 I have actions like { type:"GOAL_UP", team:"red" } for which the reducer will change the corresponding value (score) in the state.我有像{ type:"GOAL_UP", team:"red" }这样的操作,reducer 将更改状态中的相应值(分数)。

Now I want to add an epic that whenever it encounters a GOAL_UP checks if one of the two teams has won the game.现在我想添加一个史诗,每当遇到GOAL_UP时,它GOAL_UP检查两支球队中的一支是否赢得了比赛。 If a team has reached a score of 8, I want it to dispatch a GAME_WON action.如果一个团队的得分达到了 8,我希望它发出GAME_WON动作。 My problem is that I have to access the state to know the score of each team.我的问题是我必须访问状态才能知道每个团队的得分。 I cannot find a way to access the state from within an epic.我找不到从史诗中访问状态的方法。

Is accessing the state out of scope for redux-observable?访问状态是否超出了 redux-observable 的范围? If so, how would you solve this problem?如果是这样,你会如何解决这个问题?

I am thinking about having the new score as a property in the GOAL_UP action.我正在考虑将新分数作为GOAL_UP操作中的一个属性。 Something like this { type:"GOAL_UP", team:"red", newScore:8 } .类似这样的{ type:"GOAL_UP", team:"red", newScore:8 } But somehow this feels wrong.但不知何故,这感觉不对。 I also have two physical buttons on the fusball-table that are connected with web sockets, which dispatch the GOAL_UP action.我还在 fusball-table 上有两个物理按钮,它们与网络套接字连接,它们调度GOAL_UP动作。 Since they are not triggered from my ui they don't know about the state.由于它们不是从我的 ui 触发的,因此它们不知道状态。

I could also think of dispatching the GAME_WON action in the render function of my ui component if the score of either team is 8. But that feels even more wrong :)如果任一团队的得分为 8,我也可以考虑在我的 ui 组件的渲染函数中调度GAME_WON动作。但这感觉更错误:)

Input is very welcome, Thanks非常欢迎输入,谢谢

Check out the Accessing the Store's State section of the redux-observable docs.查看 redux-observable 文档的Accessing the Store's State部分。

It describes how, in addition to a stream of actions (the first argument), your epics receive a stream of store states (second argument).它描述了除了动作流(第一个参数)之外,您的史诗如何接收存储状态流(第二个参数)。

Use state$.value to synchronously access the current state.使用state$.value同步访问当前状态。 This value will contain your entire redux store state.此值将包含您的整个redux 存储状态。

Note that by the time an action reaches your epic it has already be run through your reducers (and any state updated).请注意,当一个动作到达您的史诗时,它已经通过您的减速器运行(以及更新的任何状态)。

const INCREMENT = 'INCREMENT';
const INCREMENT_IF_ODD = 'INCREMENT_IF_ODD';

const increment = () => ({ type: INCREMENT });
const incrementIfOdd = () => ({ type: INCREMENT_IF_ODD });

const incrementIfOddEpic = (action$, state$) =>
  action$.ofType(INCREMENT_IF_ODD)
    .filter(() => state$.value.counter % 2 === 0)
    .map(increment);

// later...
dispatch(incrementIfOdd());

I would also add that as described, I don't think you need redux-observable or even redux for this--it's rather overkill.我还要补充一点,如上所述,我认为您不需要 redux-observable 甚至 redux 来实现这一点——这有点矫枉过正。 Of course, if this is just to get your feet wet and learn, by all means!当然,如果这只是为了让你的脚湿透和学习,无论如何! Just don't want you to think you need these complex abstractions for simple things.只是不想让您认为您需要这些复杂的抽象来处理简单的事情。

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

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