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
How to run an action from outside a connected component #76
Comments
Agreed. I wish |
Might be worth taking a peek at Stockroom. It extends Unistore with this functionality in order to make actions centralized (since they're running in a single Web Worker). The key is to replace |
It seems like we have a fairly decent surface area for this already. Most notably, it's a bit hard to provide a simple API for bindActions, since it requires a Store reference due to the store not being a singleton. I tend to use things like your example above, or just throw the .action() call inline: import { add, subtract } from './actions';
store.setState({ value: 5 })
store.action(add)(5)
store.getState().value // 10 |
The following is not really a 'pattern' as such, but it's certainly a simple way that's supported well by the API: const store = createStore({
lols: 5,
});
const actions = (store) => ({
myCoolAction(state) {
return { ...state, lols: state.lols+1 }
},
});
store.setState(actions(store).myCoolAction(store.getState())); |
Here's the pattern I settled on: const store = createStore({
ready: false,
lols: 5,
});
const actions = (store) => ({
increment(state) {
return {...state, lols: state.lols+1 }
},
setReady(state, ready) {
return {...state, ready: ready, }
},
});
function act(store, actionsObj, action, ...args) {
return store.setState(
actionsObj[action](
store.getState(),
...args));
}
const actor = act.bind(null, store, actions(store));
const boundActions = Object.assign({},
...Object.keys(actions(store))
.map(action => ({
[action]: actor.bind(null, action),
})),
);
boundActions.increment();
boundActions.setReady(true); |
Similar to #75
You sometimes need to run an action from outside a connected component. I haven't seen a recommended pattern to do this so have come up with a temporary solution
This is basically mapActions but scoped to the current module's actions
This also works, but uses a string to reference the action name
Any thoughts on a nice way to do this?
The text was updated successfully, but these errors were encountered: