Skip to content

Instantly share code, notes, and snippets.

@darrenjaworski
Created August 18, 2018 17:11
Show Gist options
  • Save darrenjaworski/25f866d9e5ea20a9d2605de0c535ec05 to your computer and use it in GitHub Desktop.
Save darrenjaworski/25f866d9e5ea20a9d2605de0c535ec05 to your computer and use it in GitHub Desktop.
redux createstore
// basic instance of store in redux
const createStore = reducer => {
let state;
let listeners = [];
const getState = () => state;
const dispatch = action => {
state = reducer(state, action);
listeners.forEach(listener => listener());
};
const subscribe = listener => {
listeners.push(listener);
return () => {
listeners = listeners.filter(l => l !== listener);
};
};
dispatch({});
return { getState, dispatch, subscribe };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment