Skip to content

Instantly share code, notes, and snippets.

@jasonleehodges
Created October 19, 2019 21:37
Show Gist options
  • Save jasonleehodges/299873932855a5bae12ec2bd675c88a2 to your computer and use it in GitHub Desktop.
Save jasonleehodges/299873932855a5bae12ec2bd675c88a2 to your computer and use it in GitHub Desktop.
Custom react-redux hooks
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