Comments (1)
Main.js
문제상황
광고 목록보다 글 목록을 먼저 불러올 경우, 해당 오류가 발생함. 문제는 useEffect 위치를 광고 목록부터 받아왔음에도 문제가 발생된다는 점이다.
이전 코드
const fetchPostList = async (pageNum, order, categoryNum, limitNum) => {
const temp = await getPostList(pageNum, order, categoryNum, limitNum);
setList(temp);
};
const fetchADList = async (pageNum, limitNum) => {
const temp = await getADList(pageNum, limitNum);
setAdList(temp);
};
useEffect(() => {
fetchPostList(1, order, type, limit);
}, [type, limit, order]);
useEffect(() => {
fetchADList(1, limit);
}, [limit]);
// 중략
return (
<main>
<div>
<Nav />
<div className="main__wrapper">
<div className="main__login">
<span>로그인</span>
</div>
<div className="main__container">
<Filter
type={type}
setType={setType}
order={order}
setOrder={setOrder}
/>
{list.map((item, i) => (
<>
<글목록 컴포넌트>
{checkIsAd(i) ? <광고 컴포넌트 /> : null}
</>
))}
</div>
</div>
</div>
</main>
);
변경된 코드
return (
<main>
<div>
<Nav />
<div className="main__wrapper">
<div className="main__login">
<span>로그인</span>
</div>
<div className="main__container">
<Filter
type={type}
setType={setType}
order={order}
setOrder={setOrder}
/>
{list.map((item, i) => (
<>
<글목록 컴포넌트>
{adList.length > 0 && checkIsAd(i) ? (<광고 컴포넌트 />) : null}
))}
</div>
</div>
</div>
</main>
);
해결방법
-
광고 목록에 관한 useEffect의 호출 순서를 글 목록보다 높게 위치시켰음에도 글 목록이 먼저 렌더링되는 까닭은 네트워크 처리 시간의 차이라고 짐작된다.
-
위와 같이 adList.length가 0일 때만 렌더링되도록 설정해줌으로써, 광고 리스트를 불러온 뒤에 렌더링되도록 설정해줌으로써 간단하게 해결할 수 있었음.
react hooks에서 호출 순서와 관련된 자세한 내용은 아래 링크 참조
https://blog.logrocket.com/post-hooks-guide-react-call-order/
from hello-world.
Related Issues (4)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hello-world.