Created
November 24, 2021 17:11
-
-
Save mkuklis/eb740f46be4e7bb085051e97a57f8bf4 to your computer and use it in GitHub Desktop.
zustand middleware for actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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