developit / developit/unistore

How to run an action from outside a connected component

Open
#76 5 comments 1 reaction 0 assignees View on GitHub
question
Dominant language
JavaScript
Stars
2.8k
Forks
134
PR merge metrics
No merged PRs in 30d

Description

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](https://github.com/developit/unistore/blob/master/src/util.js#L2) but scoped to the current module's actions

```js
export function bindActions(store) {
const innerActions = actions(store);
let mapped = {};
for (let i in innerActions) {
mapped[i] = store.action(innerActions[i]);
}
return mapped;
}

// usage
bindActions(store).setActiveOrder(order);
```

This also works, but uses a string to reference the action name
```js
export const runAction = (store, actionName, args) => store.action(actions(store)[actionName])(args);

// usage
runAction(store, 'setActiveOrder', order);
```

Any thoughts on a nice way to do this?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.