Last active
October 24, 2021 00:29
-
-
Save wildseansy/faea34beb2cdc56dfbd740bc6536f6d9 to your computer and use it in GitHub Desktop.
[Redux Migration Example] Step 2
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
import * as React from "react" | |
import { useSelector, useDispatch } from "react-redux" | |
// UserItem represents a user item in the list | |
import UserItem from "app/components/UserItem"; | |
type RootReducer = { | |
users: UserReducerState; | |
} | |
const getUserState = (state: RootReducer): UserReducerState => state.users; | |
const getUsers = createSelector( | |
getUserState, | |
(userState): User[] => Object.values(userState) | |
); | |
const UsersList: React.FC = () => { | |
const users = useSelector(getUsers); | |
const dispatch = useDispatch(); | |
const onDelete = (user: User) => { | |
dispatch({ type: "DELETE_USER", payload: user }); | |
} | |
const onUpdate = (user: User) => { | |
dispatch({ type: "UPDATE_USER", payload: user }); | |
} | |
return users.map((user: User) => { | |
return <UserItem key={user.id} onTapDelete={onDelete} onUpdated={onUpdate} />; | |
}); | |
} | |
export default UsersList; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment