Skip to content

Instantly share code, notes, and snippets.

@adamkleingit
Last active November 8, 2018 06:32
Show Gist options
  • Save adamkleingit/0f3b71e3d1039fda55b88bcd6bfb0e1c to your computer and use it in GitHub Desktop.
Save adamkleingit/0f3b71e3d1039fda55b88bcd6bfb0e1c to your computer and use it in GitHub Desktop.
Experimenting with Hooks and Redux
const useStore = () => {
const { store } = useContext(ReactReduxContext);
return store;
};
const useSelector = selector => {
const store = useStore();
const prevState = selector(store.getState());
const [selected, setSelected] = useState(prevState);
useEffect(() => {
return store.subscribe(() => {
const nextState = selector(store.getState());
if (nextState !== prevState) {
prevState = nextState;
setSelected(nextState);
}
});
}, []);
return prevState;
};
const useAction = action => {
const store = useStore();
return (...args) => store.dispatch(action(...args));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment