-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b939aa
commit 4f2e5a6
Showing
19 changed files
with
407 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
|
||
const User = ({ user }) => { | ||
const { email, name, username } = user; | ||
return ( | ||
<div> | ||
<h1> | ||
{username} ({name}) | ||
</h1> | ||
<p> | ||
<b>e-mail:</b> {email} | ||
</p> | ||
</div> | ||
); | ||
}; | ||
|
||
export default User; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React, {useEffect} from 'react'; | ||
import { useSelector, useDispatch } from 'react-redux'; | ||
import User from '../components/User'; | ||
import { usePreloader } from '../lib/PreloadContext'; | ||
import { getUser } from '../modules/users'; | ||
|
||
const UserContainer = ({ id }) => { | ||
const user = useSelector(state => state.users.user); | ||
const dispatch = useDispatch(); | ||
|
||
usePreloader(() => dispatch(getUser(id))); // 서버 사이드 렌더링을 할 때 API 호출하기 | ||
useEffect(() => { | ||
if (user && user.id === parseInt(id, 10)) return; // 사용자가 존재하고, id가 일치한다면 요청하지 않음 | ||
dispatch(getUser(id)); | ||
}, [dispatch, id, user]); // id가 바뀔 떄 새로 요청해야 함 | ||
|
||
// 컨테이너 유효성 검사 후 return null을 해야 하는 경우에 | ||
// null 대신 Preloader 반환 | ||
if (!user) { | ||
return null; | ||
} | ||
return <User user={user} />; | ||
}; | ||
|
||
export default UserContainer; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React, { useEffect } from 'react'; | ||
import Users from '../components/Users'; | ||
import { connect } from 'react-redux'; | ||
import { getUsers } from '../modules/users'; | ||
import { Preloader } from '../lib/PreloadContext'; | ||
|
||
// const { useEffect } = React; | ||
|
||
const UsersContainer = ({ users, getUsers }) => { | ||
// 컴포넌트가 마운트되고나서 호출 | ||
useEffect(() => { | ||
if (users) return; // users가 이미 유효하다면 요청하지 않음 | ||
getUsers(); | ||
}, [getUsers, users]); | ||
return ( | ||
<> | ||
<Users users={users} /> | ||
<Preloader resolve={getUsers}/> | ||
</> | ||
); | ||
}; | ||
|
||
export default connect( | ||
state => ({ | ||
users: state.users.users | ||
}), | ||
{ | ||
getUsers | ||
} | ||
)(UsersContainer); |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { createContext, useContext } from 'react'; | ||
|
||
// 클라이언트 환경: null | ||
// 서버 환경: { done: false, promises: [] } | ||
const PreloadContext = createContext(null); | ||
export default PreloadContext; | ||
|
||
// resolve는 함수 타입. | ||
export const Preloader = ({ resolve }) => { | ||
const preloadContext = useContext(PreloadContext); | ||
if (!preloadContext) return null; // context 값이 유효하지 않다면 아무것도 하지 않음 | ||
if (preloadContext.done) return null; // 이미 작업이 끝났다면 아무것도 하지 않음 | ||
|
||
// promises 배열에 프로미스 등록 | ||
// resolve 함수가 프로미스를 반환하지 않더라도, 프로미스 취급을 하기 위해 | ||
// Promise.resolve 함수 사용 | ||
preloadContext.promises.push(Promise.resolve(resolve())); | ||
return null; | ||
}; | ||
|
||
// Hook 형태로 사용할 수 있는 함수 | ||
export const usePreloader = resolve => { | ||
const preloadContext = useContext(PreloadContext); | ||
if (!preloadContext) return null; | ||
if (preloadContext.done) return null; | ||
preloadContext.promises.push(Promise.resolve(resolve())); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { combineReducers } from "redux"; | ||
import users from './users'; | ||
import users, { usersSaga } from './users'; | ||
import { all } from 'redux-saga/effects' | ||
|
||
export function* rootSaga() { | ||
yield all([usersSaga()]); | ||
} | ||
|
||
const rootReducer = combineReducers({ users }); | ||
export default rootReducer; |
Oops, something went wrong.