-
-
Notifications
You must be signed in to change notification settings - Fork 191
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
Generalising effects/actions/data #36
Comments
I've been using a function that takes the name and the url to fetch (that would be the function to call for you, {
[name]: {
list: [],
loadAll: effect(...) => {
...
dispatch.helper[name].setList(...
},
} and then spreading those const model = {
...materialState,
...formState,
}, But I agree with you, one would want to easily parse the local state, but it is also important to be able to dispatch elsewhere. |
@RuedigerLudwig I have opened a PR which may help you with this. Utilising the meta data exposed to the effect function you could do something like: const list = (endpoint) => ({
list: [],
ids: select(state => R.pluck('id', state.list)),
loaded: select(state => state.list === []),
setList: (state, payload) => ({ ...state, list: payload }),
loadAll: effect(async (dispatch, _payload, _getState, _injections, meta) => {
const data = await endpoint()
const setList = R.path(meta.parent.join('setList'), dispatch);
setList(data);
}),
}) And then use it like so: const store = {
helper: {
material: {
other: '👋',
...list(getAllMaterialien)
},
form: {
...list(getAllFormen)
},
},
} Let me know what you think. 😊 @adrienWeiss - would appreciate your input too. |
The only thing I dislike about this helper approach is that it can be harder to visualise your state tree, however, when the typescript work lands you should get typesafe autocomplete on your model which will help dramatically. :) |
This PR adds a `meta` parameter to effects. This parameter exposes both the parent path and the path of the effect being actioned. ```javascript const store = createStore({ foo: { doSomething: effect((dispatch, payload, getState, injections, meta) => { console.log(meta); // { // parent: ['foo'], // path: ['foo', 'doSomething'] // } }) } }); await store.dispatch.foo.doSomething() ``` This should open mechanism by which to alleviate issues described within #36.
Available in latest release. https://github.com/ctrlplusb/easy-peasy/releases/tag/v1.7.0 🤘 |
Wow, that is quick. Thank you for the quick addition. I am pretty sure that it solves my use case. |
This PR adds a `meta` parameter to effects. This parameter exposes both the parent path and the path of the effect being actioned. ```javascript const store = createStore({ foo: { doSomething: effect((dispatch, payload, getState, injections, meta) => { console.log(meta); // { // parent: ['foo'], // path: ['foo', 'doSomething'] // } }) } }); await store.dispatch.foo.doSomething() ``` This should open mechanism by which to alleviate issues described within #36.
Hello,
first: great and easy to use library. Thank you very much for it
I came along a small problem when trying to reuse my code. I have some state trees, that look very similar to each other. In general they all look the same: Some data, some actions, some selects and some effects. In the effects I need to call actions via the dispatch.
Here the ability to reuse stops, because I need to call the set method with an absolute path and not a relative one. But I might not know in general where and how deep I might be in the "store tree".
Is there an elegant way to solve this?
Cheers
Rüdiger
The text was updated successfully, but these errors were encountered: