Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full state from the nested action (question) #712

Closed
dmitry-kurmanov opened this issue Jun 28, 2018 · 6 comments
Closed

Full state from the nested action (question) #712

dmitry-kurmanov opened this issue Jun 28, 2018 · 6 comments
Labels
question How do I do that?

Comments

@dmitry-kurmanov
Copy link

Hello! Thanks for the project. I think it is great!

Is there way to get the full state (global) from nested actions ?

May be I should use nested actions only if I want totaly isolated part of my hyper app.

Could anyone tell about best practicies?

@zaceno
Copy link
Contributor

zaceno commented Jun 28, 2018

@dmitrykurmanov

You're right, you should try not to need global state in your nested actions.

What does happen when you're using nested actions, is you sometimes want to call another action in another namespace (or in the "root" namespace). For that, I have a technique explained here:

https://zaceno.github.io/hypercraft/post/cross-namespace-action-calling/

I think, with this technique, you shouldn't need access to the global state, because instead you can call actions in the global namespace from the nested actions.

If you really want global state in a nested action, create an action in the global namespace like this:

const actions = {
  withGlobalState: f => state => f(state)
  ...
}

Now you can use the technique described above to access withGlobalState from the nested action (because it's stored on the local state). So it would look like:

const actions = {
   ...
  namespace: {
    subaction: _ => state => {
      state.withGlobalState(globalState => {
        /* Here you have access to globalState */
      }),
   }
 }
 ...
}

... but I hope you don't have to do that 😜 There must surely be a more clean and readable way to achieve what you need.

@jorgebucaran
Copy link
Owner

Hi @dmitrykurmanov!

Nope, there's no way to get the state from a nested action. Please consider @zaceno's advice in the meantime or until 2.0 is out.

In 2.0 (#672 #696) actions are flat and unwired (they take the global state) and slices are no more, so this won't be a problem going forward.

The new "problem" will be how to slice the state like we do in 1.0. This will be possible via an optional library.

Cheers!

@jorgebucaran jorgebucaran added the question How do I do that? label Jun 29, 2018
@dmitry-kurmanov
Copy link
Author

Thank you guys for the answer! I just try to create some components with hyperapp and without nested actions my actions will be more complex, for example:
with nested acctions:

      // partial state = fullState.nodes 
      const node = state[id];
      const isExpand = !node.isExpand;
      return {
        ...state,
        [id]: {
          ...node,
          isExpand
        }
      };

and without:

      // fullState
      const node = state.nodes[id];
      const isExpand = !node.isExpand;
      return {
        ...state,
        nodes: {
          ...state.nodes,
            [id]: {
              ...node,
              isExpand
            }
       }
    };

It is not the big problem in fact :)

Anyway if in ha2.0 the problem will gone it is ok for me. And I think that you should not be distracted by this.

@jorgebucaran
Copy link
Owner

@dmitrykurmanov Slicing the state in 2.0 will be possible via an external library. It will not be exactly the same as 1.0, but I'm more excited about it being optional than introducing more features to the framework.

@dmitry-kurmanov
Copy link
Author

@jorgebucaran sounds great!

@okwolf
Copy link
Contributor

okwolf commented Jun 30, 2018

@jorgebucaran Slicing the state in 2.0 will be possible via an external library.

Do we plan to provide that library or will that be on the community to provide?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question How do I do that?
Projects
None yet
Development

No branches or pull requests

4 participants