Coder Social home page Coder Social logo

Comments (1)

joonfluence avatar joonfluence commented on July 24, 2024

Main.js

image

문제상황

광고 목록보다 글 목록을 먼저 불러올 경우, 해당 오류가 발생함. 문제는 useEffect 위치를 광고 목록부터 받아왔음에도 문제가 발생된다는 점이다.

image

이전 코드

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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.