Skip to content

Instantly share code, notes, and snippets.

@mkuklis
Created November 24, 2021 17:11
Show Gist options
  • Save mkuklis/eb740f46be4e7bb085051e97a57f8bf4 to your computer and use it in GitHub Desktop.
Save mkuklis/eb740f46be4e7bb085051e97a57f8bf4 to your computer and use it in GitHub Desktop.
zustand middleware for actions
// Extends API with actions
const actionsMiddleware = fn => (set, get, api) => {
const _actions = {};
api.actions = () => _actions;
const state = fn(set, get, api);
Object.keys(state).forEach(key => {
if (typeof state[key] === 'function') {
_actions[key] = state[key];
}
});
return state;
};
// usage
store.actions();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment