인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

junggyoo14104's profile image
junggyoo14104

asked

Practical React Programming

Passing data to the context API

caution.js 렌더링 확인 좀 부탁드립니다.

Written on

·

277

1

import React, { createContext, useState, useContext } from "react";

const UserConText = createContext({ username: "unknown", age: 0 });

export default function App() {
  const [user, setUser] = useState({ username: "마이크", age: 23 });
  const [count, setCount] = useState(0);
  console.log("App render");
  return (
    <div>
      <UserConText.Provider value={user}>
        <Profile />
        <button onClick={() => setCount(count + 1)}>증가</button>
      </UserConText.Provider>
    </div>
  );
}

const Profile = React.memo(function () {
  console.log("Profile render");
  return (
    <div>
      <Greeting />
    </div>
  );
});

function Greeting() {
  console.log("Greeting render");
  const { username } = useContext(UserConText);
  return <p>{`${username}님 안녕하세요`}</p>;
}
reduxreact

Answer 3

1

junggyoo14104님의 프로필 이미지
junggyoo14104
Questioner

네 맞아요! StrictMode 지우고 하니까 한 번씩 렌더링 되네요 ㅎㅎ 빠른 답변 감사합니다! 

1

landvibe님의 프로필 이미지
landvibe
Instructor

안녕하세요
혹시 create-react-app 인가요?
맞다면 index.js 쪽을 보면 아마 StrictMode 컴포넌트가 있을거에요
StrictMode 를 지우고 해보시겠어요?
참고로 StrictMode 가 있으면 concurrent mode 대응을 위해 리액트가 고의로 두 번씩 렌더링 합니다

1

junggyoo14104님의 프로필 이미지
junggyoo14104
Questioner

App render 가 콘솔로그에 2번씩 찍히면서 렌더링이 2번씩 발생되고 있는데 이유를 알 수 있을까요? 

junggyoo14104's profile image
junggyoo14104

asked

Ask a question