[英]What's the alternative to store a function in a Redux store
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路由器)呈现的路由做出不同的操作
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. 我的第一个想法是将一个函数保存在商店中,并使用
mapStateToProps
和connect
,将该函数作为道具发送给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问题也讨论了这个问题。
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: 几个选项:
"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-saga
或redux-observable
,可让您响应已调度的操作。 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
. 然后,当导航状态更改时,您可以从
NavigationBar
或ActionButton
内访问当前路线。
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.