简体   繁体   English

在Redux存储中存储功能的替代方法是什么

[英]What's the alternative to store a function in a Redux store

Problem 问题

I have a structure like this: 我有这样的结构:

<MenuBar />
<NavigationBar>
  <irrelevant stuff here />
  <ActionButton />
</NavigationBar>
<Router>
  <Route ...>
</Router>

And I want the ActionButton in the NavigationBar to do something different depending on what Route is rendered by the Router (a standard ReactRouter Router) 而且我希望NavigationBar中的ActionButton根据路由器(标准ReactRouter路由器)呈现的路由做出不同的操作

First thought 最初的想法

My first thought was to save a function on the store and using mapStateToProps and connect , send that function as a prop to the ActionButton. 我的第一个想法是将一个函数保存在商店中,并使用mapStateToPropsconnect ,将该函数作为道具发送给mapStateToProps I would dispatch an action with the function as payload whenever the callback should change. 每当回调发生变化时,我都会调度一个功能为有效载荷的动作。

I'm pretty sure that should work, but I know storing non-serialisable stuff in the store can have negative consequences. 我很确定这应该可行,但是我知道在商店中存储不可出售的东西会带来负面影响。 In particular it gives problems with persisting and rehydrating the contents of a store, as well as interfering with time-travel debugging. 特别是,它给存储内容的持久化和重新补充水分以及干扰时间旅行调试带来了问题。 This link from the docs talks about it and has a reference to some GitHub issues where that is discussed. 文档中的此链接对此进行了讨论,并引用了讨论该问题的一些GitHub问题。 This StackOverflow question also discuss the issue. 这个StackOverflow问题也讨论了这个问题。

Question

I've seen many places where people say "you shouldn't do that", but I haven't seen an alternative to storing a function in the store yet. 我见过很多人说“你不应该这样做”的地方,但是我还没有看到在商店中存储函数的替代方法。 The alternative that I can think of is using props drilling, but I would really like to avoid that pattern. 我可以想到的替代方法是使用道具钻孔,但是我真的很想避免这种模式。

At this point I don't really need to persist or rehydrate the store, and time-travel debugging is a very nice to have but not a must-have. 在这一点上,我真的不需要持久化或重新存储商店,并且进行时间旅行调试非常好,但不是必须的。 However, I'd like to avoid sacrificing those things if there's a good alternative. 但是,如果有不错的选择,我想避免牺牲这些东西。

Thanks in advance! 提前致谢! :) :)

A couple options: 几个选项:

  • Use a middleware to listen for an "ACTION_BUTTON_CLICKED" action, and run different logic based on other values (store state, route, etc). 使用中间件侦听"ACTION_BUTTON_CLICKED"动作,并根据其他值(存储状态,路线等)运行不同的逻辑。 This could be a custom middleware you've written yourself, or a general-purpose side effects middleware like redux-saga or redux-observable that lets you respond to dispatched actions. 这可以是您自己编写的自定义中间件,也可以是通用副作用中间件,例如redux-sagaredux-observable ,可让您响应已调度的操作。
  • Put a predefined action into the store based on other behaviors, and have that action be dispatched when the button is clicked. 根据其他行为将预定义的动作放入存储中,并在单击按钮时调度该动作。

I show an example of the predefined action approach in my post Practical Redux, Part 10: Managing Modals and Context Menus . 在我的实践Redux,第10部分:管理模态和上下文菜单中,展示了预定义操作方法的示例。

You could put the state of react-router in your redux store. 您可以将react-router的状态放入您的redux存储中。 Then when navigation state changes, you can access the current route from within the NavigationBar or the ActionButton . 然后,当导航状态更改时,您可以从NavigationBarActionButton内访问当前路线。

You could store logic for deciding what the button does based on the route in the ActionButton or the NavigationBar. 您可以存储逻辑,以根据ActionButton或NavigationBar中的路线来确定按钮的作用。

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

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