Using Redux with the Interactivity API #52939
luisherranz
started this conversation in
Interactivity API
Replies: 1 comment
-
Given the context of WP, I imagine consuming/integrating with |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It will be less performant (it will cause unnecessary rerenders and the JS size will increase) and compatibility with directives won't be guaranteed, so it should be used only as a migration path when there is an existing Redux store.
@DAreRodz and I have been testing patterns to consume state from Redux with the Interactivity API. It's certainly possible, although it has some caveats because you cannot use their hooks (
useSelector
,useDispatch
) in all the directives.To make it work the first thing you need to do is to add a new directive that injects the provider in the tree:
Then, you need to add it to the root element of your block:
Now, you can access
useSelector
anduseDispatch
in your callbacks.Using
useSelector
in thestate
is easy, you just have to create a function and use it inside:This is not technically
state
, so maybe it'd be better to add it toselectors
instead. Also, it cannot be used inside actions for the reasons I explain below.Using
useDispatch
is more difficult because thewp-on
callbacks don't run in the scope of the component, but in the event handler and therefore cannot use hooks.Just to give you an idea of how this works internally, it'd be something like this (simplified):
These are all the options that we explored:
1. Create a new
wp-redux-on
directiveThis directive runs the
useDispatch
in the scope of theDirectives
component and passes it down to the action:Then, the action can access
dispatch
:Stackblitz
2. Pass the
dispatch
function using a scope object shared across all the directivesAnother idea is to create a
scope
object that is shared across all the directives that run in a single element. Using it, you could usewp-evaluate
to runuseDispatch
and add it to thescope
. Then, the action would get it fromscope.dispatch
.The introduction of a
wp-evaluate
directive and how it works is explained here.Stackblitz
3. Create a wp-use-dispatch directive to share the dispatch using the scope
The previous approach could be simplified using a
data-wp-use-dispatch
directive instead of the customdata-wp-evaluate
.Stackblitz
4. Use a wp-hook directive to populate hooks
Instead of a
data-wp-use-dispatch
directive, there could be a generaldata-wp-hook
directive with another shared object calledhooks
instead ofscope
.Stackblitz
5. Use
wp-evaluate
to add event handlers manuallyAnother option is to use
wp-evaluate
to run the hook and add the event handler manually.Stackblitz
6. Allow
wp-on
to run in the scope of the component, and use the returned function as event handlerThe last option that came to my mind is to use a pattern similar to
useEffect
inwp-on
, so you can share scope between the function that runs in the component and the event handler.Stackblitz
A similar issue will happen with accessing
useSelector
inside an event handler:For that reason, I think the options that will allow any hook (5. and 6.) are superior.
Option 5:
Option 6:
Beta Was this translation helpful? Give feedback.
All reactions