bookshelf/INSTRUCTIONS.md at exercises/10-render-as-you-fetch · kentcdodds/bookshelf (github.com)
GitHub - kentcdodds/bookshelf: Build a ReactJS App workshop
Build a ReactJS App workshop. Contribute to kentcdodds/bookshelf development by creating an account on GitHub.
github.com
렌더링 하면서 필요한 데이터 가져오기
배경
- 로드 중인 모든 항목을 살펴보세요.
- 꼭 필요한 리소스들을 식별합니다.
- 가능한 한 빨리 로드를 시작하세요.
더 나은 성능을 위해선 nextjs나 gatsby를 쓰는게 좋음
(리액트 렌더링 결과로 완성된 html과 js를 분리해서 보내줌.)
사용자가 애플리케이션에 접속하면.
- index.html을 가져옴
- <script src="...">, ,<link rel="stylesheet" href="...">을 파싱해서 각 리소스를 가져옴
- JS를 만다면 자바스크립트를 파싱 및 평가함
- Call into all the modules (in import order)
- 여기에서 필요한 데이터들을 가져옴 (리액트 컴포넌트 밖으로 뺌)
- Create a bunch of React components
- Render those components with ReactDOM.render. At this point, React calls into all of our components to get the react elements
- Our components are "mounted" by React and our useEffect callbacks are called
- We make a fetch to get the logged in user's information and the AuthProvider displays a spinner while we wait.
- 이 부분을 최적화 함.
- The user's information comes back successfully, so now we render the Authenticated app (luckily we've pre-fetched that thanks to the webpack magic comment)
- We make a fetch to get the user's list items. We show an empty list while we wait.
- The list items come back and we render those list items.
즉 자바스크립트 실행이 실행되자마자 비동기로 데이터를 가져오게 하면 된다.
예제
GitHub - kentcdodds/bookshelf: Build a ReactJS App workshop
Build a ReactJS App workshop. Contribute to kentcdodds/bookshelf development by creating an account on GitHub.
github.com
아래 코드는 bootstrap API에서 처음 화면에 보여주는 모든 데이터를 다 가져온다.
해당 함수는 리액트 컴포넌트 밖에 위치하며 밖에서 호출됨을 주의한다.
그 다음 해당 데이터가 중복되는 API 별로 캐시를 초기화하고 있다.
해당 함수의 호출 결과는 promise다. 해당 promise는 비동기 훅 내부에서 resolve 해준다.
(react-query의 특징)
async function bootstrapAppData() {
let user = null
const token = await auth.getToken()
if (token) {
const data = await client('bootstrap', {token})
queryCache.setQueryData('list-items', data.listItems, {
staleTime: 5000,
})
// Let's also set the books in the query cache as well
for (const listItem of data.listItems) {
setQueryDataForBook(listItem.book)
}
user = data.user
}
return user
}
const appDataPromise = bootstrapAppData()
// Function Component...
bookshelf/hooks.js at 5214872999c04f68e95244057a5bd2eadac09140 · kentcdodds/bookshelf (github.com)
아래 코드는 비동기 처리 훅 내부에서, 위의 프로미스를 reslving
// 비동기 함수 호출 부분에서 프로미스를 resolve한다.
const run = React.useCallback(
promise => {
if (!promise || !promise.then) {
throw new Error(
`The argument passed to useAsync().run must be a promise. Maybe a function that's passed isn't returning anything?`,
)
}
safeSetState({status: 'pending'})
return promise.then(
data => {
setData(data)
return data
},
error => {
setError(error)
return Promise.reject(error)
},
)
},
[safeSetState, setData, setError],
)
'FrontEnd' 카테고리의 다른 글
[Epic React][Build an Epic React App][Testing Hooks and Components][훅과 컴포넌트 테스트] (3) | 2022.01.02 |
---|---|
[Epic React][Build an Epic React App][Unit Test][단위 테스트] (0) | 2022.01.02 |
[Epic React][Build an Epic React App][Performance] (0) | 2022.01.01 |
[Epic React][Build an Epic React App][Context] (0) | 2022.01.01 |
[Epic React][Build an Epic React App][Cache Management] (0) | 2022.01.01 |