在Re-frame中有3个事件函数,我可以使用reg-event-db
和reg-event-fx
来完成同样的事情。
reg-event-db
、reg-event-fx
和reg-event-ctx
之间的主要区别是什么?
在什么情况下应该使用reg-event-fx
而不是使用reg-event-db
或者反过来?
在Re-frame中有3个事件函数,我可以使用reg-event-db
和reg-event-fx
来完成同样的事情。
reg-event-db
、reg-event-fx
和reg-event-ctx
之间的主要区别是什么?
在什么情况下应该使用reg-event-fx
而不是使用reg-event-db
或者反过来?
reg-event-db
是reg-event-fx
的更专注、更有限的版本。当您的处理程序只关心db
值时,使用reg-event-db
最方便;您可以使用reg-event-fx
编写相同的处理程序,但必须从处理程序的输入中获取:db
值。这是注册事件处理程序的最常见情况。reg-event-fx
并从处理程序的输入中获取:coeffects
值(如有必要,还需获取:db
)。常见用例是在需要访问浏览器存储(例如cookies、本地存储)但希望保持处理程序无副作用时。文档提供了一个example。
reg-event-ctx
是一种更低级别的事件处理程序类型,它接收整个context,但这几乎不是您想要用于注册事件处理程序的情况。从文档中可以看到:这种形式的注册几乎从不使用。
这是一个上下文映射示例:{:coeffects {:event [:some-id :some-param]
:db <original contents of app-db>}
:effects {:db <new value for app-db>
:dispatch [:an-event-id :param1]}
:queue <a collection of further interceptors>
:stack <a collection of interceptors already walked>}
reg-event-db
处理程序只提供:coeffects -> :db
值,并且它们的返回值通知:effects -> :db
reg-event-fx
处理程序提供整个:coeffects
值,并且它们的返回值通知:effects
reg-event-ctx
处理程序传递(并返回)此整个上下文映射