Skip to content

[URH-57] useDeepCompareEffect 신규 #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 2, 2024

Conversation

bicochan
Copy link
Member

👾 Pull Request

1️⃣ Spec

  • 의존성 배열의 깊은 비교를 통해 변화를 감지하고 콜백을 실행시킵니다.

2️⃣ 변경 사항

  • isEqual() 함수를 사용하기 위해 fast-deep-equal 패키지를 추가하였습니다.
    • isEqual() 함수를 직접 작성하려면 상당한 양의 코드가 필요합니다.
    • fast-deep-equal 패키지는 852byte의 작은 번들 사이즈와 주당 46,000,000 이상 다운로드하는 패키지로 신뢰성이 높아 설치해봤습니다.

3️⃣ 예시 코드

function App() {
  const [, setCount] = useState(0);
  const effectCountRef = useRef(0);
  const deepCompareCountRef = useRef(0);

  useEffect(() => {
    effectCountRef.current += 1;
  // 빈 객체는 매번 새로운 참조를 갖기 때문에 effectCount는 계속 증가
  }, [{}]);

  useDeepCompareEffect(() => {
    deepCompareCountRef.current += 1;
  // 빈 객체를 같은 값으로 판단해 deepCompareCountRef는 한 번(첫 시도)만 증가
  }, [{}]);

  return (
    <div>
      <p>effectCount: {effectCountRef.current}</p>
      <p>deepCompareCount: {deepCompareCountRef.current}</p>
      <p>
        <button type="button" onClick={() => setCount((c) => c + 1)}>
          reRender
        </button>
      </p>
    </div>
  );
}

4️⃣ 관련 문서 (선택 사항)

Copy link
Member

@jeongbaebang jeongbaebang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@bicochan bicochan requested a review from Choozii August 29, 2024 11:55
@bicochan
Copy link
Member Author

@Choozii 지예 님 리뷰 한 번만 부탁드려요!

Copy link
Member

@Choozii Choozii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리뷰가 늦었네요... 수고하셨습니다 🆒 🆒 🆒

*/
const useDeepCompareEffect = (
callback: () => void,
dependencies: unknown[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P5] 결국에 type 자체는 똑같기는 한데 React.DependencyList라는 타입이 있나바유!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 그 타입을 사용해 보려고 했었는데 unknown[]으로 작성하는 게 더 직관적이라고 판단해 봤습니다...!

ps. import 수를 줄이고 싶은 욕심도 한스푼...😅

@bicochan bicochan merged commit d15ea4f into master Sep 2, 2024
4 checks passed
@bicochan bicochan deleted the URH-57/use-deep-compare-effect branch September 2, 2024 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants