Skip to content

Commit

Permalink
6.4.2 react-redux 패키지 사용하기
Browse files Browse the repository at this point in the history
  • Loading branch information
b-chae committed Oct 22, 2021
1 parent 807054a commit 360ccae
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 19 deletions.
168 changes: 168 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 @@ -9,6 +9,7 @@
"immer": "^9.0.6",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-redux": "^7.2.5",
"react-scripts": "4.0.3",
"redux": "^4.1.1",
"web-vitals": "^1.1.2"
Expand Down
19 changes: 4 additions & 15 deletions src/friend/container/FriendMain.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
import React, { useEffect, useReducer } from "react";
import { useDispatch, useSelector } from "react-redux";
import { getNextFriend } from "../../common/mockData";
import store from "../../common/store";
import FriendList from "../component/friendList";
import { addFriend } from "../state";

export default function FriendMain() {
const [, forceUpdate] = useReducer(v => v+1, 0)
useEffect(() => {
let prevFriends = store.getState().friend.friends
const unsubscribe = store.subscribe(() => {
// 책에서와 다르게 코딩한 부분
// prevFriends !== store.friends
if (prevFriends !== store.getState().friend.friends){
forceUpdate()
}
prevFriends = store.getState().friend.friends
})
return () => unsubscribe()
}, [])
const friends = useSelector(state => state.friend.friends)
const dispatch = useDispatch()

function onAdd() {
const friend = getNextFriend()
store.dispatch(addFriend(friend))
dispatch(addFriend(friend))
}

console.log("FriendMain render")
const friends = store.getState().friend.friends
return (
<div>
<button onClick={onAdd}>친구 추가</button>
Expand Down
12 changes: 8 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import store from "./common/store";
import FriendMain from "./friend/container/FriendMain";
import TimelineMain from "./timeline/container/TimelineMain";

ReactDOM.render(
<div>
<FriendMain />
<TimelineMain />
</div>,
<Provider store={store}>
<div>
<FriendMain />
<TimelineMain />
</div>
</Provider>,
document.getElementById("root")
)

0 comments on commit 360ccae

Please sign in to comment.