Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

[redux-subspace]Being in a subspace, can I dispatch an action to an other one ? #112

Closed
remy-poirier opened this issue Jul 24, 2019 · 3 comments

Comments

@remy-poirier
Copy link

remy-poirier commented Jul 24, 2019

Is it a bug, feature request or question?

This is a question:
I am in a subspace, and I want to dispatch an action to an other subspace, without involving a global action.

To contextualize, let's say that my current subspace is tab1-main, from this subspace I want to dispatch an action in a new tab tab2-details, so my action will look like this:
type: "tab2-details/actionName, payload: {...}", without the globalAction flag. Is there any solutions to do so ?

I tried to do things like:

dispatch(globalAction(namespacedAction(subspaceId)(dispatchResult)))

In order to get out the current namespace and going into the new one, but it dispatches the action as a globalAction and it causes side effect inside my application.

I also tried to only dispatch a namespaced action like so:

dispatch(namespacedAction(subspaceId)(dispatchResult)))

But obviously it dispatches an action like tab1-main/tab2-details/actionName.

Your Setup

package version(s)
redux 4.0.0
redux-subspace 2.0.0

I hope I am clear and you have a solution

@remy-poirier remy-poirier changed the title [redux-subspace] [redux-subspace]Being in a subspace, can i dispatch an action to an other one ? Jul 24, 2019
@remy-poirier remy-poirier changed the title [redux-subspace]Being in a subspace, can i dispatch an action to an other one ? [redux-subspace]Being in a subspace, can I dispatch an action to an other one ? Jul 24, 2019
@jpeyper
Copy link
Collaborator

jpeyper commented Jul 24, 2019

Hi @remy-poirier and thanks for your question.

Sorry in advance for typos, I'm on my phone.

If I understood correctly, you've got a namespace structure similar to

parent
- tab1-main
- tab2-details

Where the tabs are direct siblings of each other (in subspace terms, not necessarily in react).

What you need is access to the parent subspace's dispatch to fire namespace action into, so it doesn't get wrapped with tab1-main

@mpeyper can probably suggest better ways to get access to that dispatch, but at a basic level you could pass it as a prop to your Tab components (assuming you're using React).

Does that make sense to you? If not I'll try to setup a code sandbox later today to demonstrate when I'm on my laptop.

Other option could also include using a custom redux middleware that can consume and redirect an action to the correct subspace (if this could be made generic enough it might fit as a new feature for subspace, as we also sometimes struggle with this same problem at IOOF). But I suspect if you could do this, you'd already have a unique enough action name to be able to use a global action.

And finally, if you have these kinds of complex interactions between subspaces, you might find yourself constantly fighting against redux-subspace's encapsulation of state and actions. redix-subspace may not be correct solution for your project (but I'm hoping we can figure something out for you).

@mpeyper
Copy link
Contributor

mpeyper commented Jul 28, 2019

Yes, @jpeyper is correct, the only way to achieve this is to us the parent subspace's dispatch function.

We recently added a new feature called "Parent Spaces" which is exactly this. Assuming you are using react, you can use the ParentSpaceProvider component or parentSpaced HOC and dispatch the namepspacedAction from a component rendered within it and it will have the expected namespace applied.

If you are not using react, there is the parentSpace helper function which can be called on any store created by the subspace function.

If you want to dispatch this from within a thunk or other middleware that provides the dispatch without the store, then you might have to get creative and write your own version of it that supports the parentSpace feature (and share the result with us so we can see if it has a place in the supported packages).

@mpeyper
Copy link
Contributor

mpeyper commented Sep 8, 2019

Closing due to inactivity. Happen to reopen if mre discusison is required.

@mpeyper mpeyper closed this as completed Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants