Skip to content

Commit

Permalink
6.5.2 reselect 패키지 사용하기
Browse files Browse the repository at this point in the history
  • Loading branch information
b-chae committed Oct 22, 2021
1 parent afc1f78 commit 8c0fb87
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 18 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"react-redux": "^7.2.5",
"react-scripts": "4.0.3",
"redux": "^4.1.1",
"reselect": "^4.0.0",
"web-vitals": "^1.1.2"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/common/store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { combineReducers, createStore } from "redux"
import friendReducer from "../friend/state"
import friendReducer from "../friend/state/state"
import { timelineReducer } from "../timeline/state"

const reducer = combineReducers({
Expand Down
21 changes: 8 additions & 13 deletions src/friend/container/FriendMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,21 @@ import { getNextFriend } from "../../common/mockData";
import { MAX_AGE_LIMIT, MAX_SHOW_LIMIT } from "../common";
import FriendList from "../component/friendList";
import NumberSelect from "../component/NumberSelect";
import { addFriend, setAgeLimit, setShowLimit } from "../state";
import { getAgeLimit, getFriendsWithAgeLimit, getFriendsWithAgeShowLimit, getShowLimit } from "../state/selector";
import { addFriend, setAgeLimit, setShowLimit } from "../state/state";

export default function FriendMain() {
const [
ageLimit,
showLimit,
friendsWithAgeLimit,
friendsWithAgeShowLimit
] = useSelector(state => {
const {friends, ageLimit, showLimit} = state.friend
const friendsWithAgeLimit = friends.filter(
friend => friend.age <= ageLimit
)
return [
ageLimit,
showLimit,
friendsWithAgeLimit,
friendsWithAgeLimit.slice(0, showLimit)
]
}, shallowEqual)
] = useSelector(state => [
getAgeLimit(state),
getShowLimit(state),
getFriendsWithAgeLimit(state),
getFriendsWithAgeShowLimit(state)
], shallowEqual)
const dispatch = useDispatch()

function onAdd() {
Expand Down
15 changes: 15 additions & 0 deletions src/friend/state/selector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createSelector } from "reselect"

export const getFriends = state => state.friend.friends
export const getAgeLimit = state => state.friend.ageLimit
export const getShowLimit = state => state.friend.showLimit

export const getFriendsWithAgeLimit = createSelector(
[getFriends, getAgeLimit],
(friends, ageLimit) => friends.filter(friend => friend.age <= ageLimit)
)

export const getFriendsWithAgeShowLimit = createSelector(
[getFriendsWithAgeLimit, getShowLimit],
(friendsWithAgeLimit, showLimit) => friendsWithAgeLimit.slice(0, showLimit)
)
8 changes: 4 additions & 4 deletions src/friend/state.js → src/friend/state/state.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import createItemsLogic from "../common/createItemsLogic"
import createReducer from "../common/createReducer"
import mergeReducers from "../common/mergeReducers"
import { MAX_AGE_LIMIT, MAX_SHOW_LIMIT } from "./common"
import createItemsLogic from "../../common/createItemsLogic"
import createReducer from "../../common/createReducer"
import mergeReducers from "../../common/mergeReducers"
import { MAX_AGE_LIMIT, MAX_SHOW_LIMIT } from "../common"

const {add, remove, edit, reducer: friendsReducer} = createItemsLogic('friends')

Expand Down

0 comments on commit 8c0fb87

Please sign in to comment.