Skip to content

Instantly share code, notes, and snippets.

@wildseansy
Last active October 24, 2021 00:29
Show Gist options
  • Save wildseansy/faea34beb2cdc56dfbd740bc6536f6d9 to your computer and use it in GitHub Desktop.
Save wildseansy/faea34beb2cdc56dfbd740bc6536f6d9 to your computer and use it in GitHub Desktop.
[Redux Migration Example] Step 2
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