Created
October 19, 2019 21:37
-
-
Save jasonleehodges/299873932855a5bae12ec2bd675c88a2 to your computer and use it in GitHub Desktop.
Custom react-redux hooks
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
import { useSelector, useDispatch } from 'react-redux'; | |
import { RootState } from '~/reducers'; | |
import { selectUser, selectAllUsers } from 'actions/app'; | |
import { setUserAction, setAllUsersAction } from '../actions/app'; | |
export const useUserState = () => { | |
return { | |
user: useSelector((state: RootState) => selectUser(state)), | |
allUsers: useSelector((state: RootState) => selectAllUsers(state)), | |
} | |
} | |
export const useUserDispatch = () => { | |
const dispatch = useDispatch(); | |
return { | |
setUser: (user: string) => () => dispatch(setUserAction(user)), | |
setAllUsers: (users: string[]) => () => dispatch(setAllUsersAction(users)), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment