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?
@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.
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!
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.
@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.
@jorgebucaran sounds great!
@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?