-
Notifications
You must be signed in to change notification settings - Fork 139
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
Show how to reuse actions within actions in docs? #75
Comments
Hmm i tried that, but my edit |
@jaredpalmer Your async action increments the count twice, FYI. @davidchase Binding of export default (store) => {
const increment = (state) => {
return {
count: state.count + 1,
}
}
const incrementAsync = (state) => {
setTimeout(() => {
store.setState(increment(state))
}, 1000)
}
return {
increment,
incrementAsync,
}
} |
Might be worth changing the readme to more explicitly spell out the outer function and calling contexts: // If actions is a function, it gets passed the store:
function createActions(store) {
const actions = {
// Actions can just return a state update:
increment(state) {
return { count: state.count + 1 };
},
// The above example as an Arrow Function:
increment2: ({ count }) => ({ count: count + 1 }),
// Async actions are actions that call store.setState():
incrementAsync(state) {
store.setState(actions.increment(state));
setTimeout(() => {
store.setState({ count: store.getState().count + 1 });
}, 1000);
}
};
return actions;
} Otherwise I'd recommend something like what @ehaynes99 suggested - avoid context entirely. |
I'm trying to avoid the wrapping/context to have my actions.js be a collection of pure functions but it makes hard to have a "thunk" like functionality.
|
This works... maybe worth adding to the readme.
The text was updated successfully, but these errors were encountered: