Last active
November 8, 2018 06:32
-
-
Save adamkleingit/0f3b71e3d1039fda55b88bcd6bfb0e1c to your computer and use it in GitHub Desktop.
Experimenting with Hooks and Redux
This file contains hidden or 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
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