Skip to content
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

useWindowResize 컴포넌트 리렌더링 문제 #10

Open
Bhanjo opened this issue Feb 21, 2023 · 0 comments
Open

useWindowResize 컴포넌트 리렌더링 문제 #10

Bhanjo opened this issue Feb 21, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@Bhanjo
Copy link
Collaborator

Bhanjo commented Feb 21, 2023

import { useEffect, useState } from 'react';

const useWindowReszie = () => {
  const [windowSize, setWindowSize] = useState({ width: null, height: null });

  useEffect(() => {
    const handleResize = () => {
      setWindowSize({ width: window.innerWidth, height: window.innerHeight });
    };

    window.addEventListener('resize', handleResize);
    handleResize();

    return () => window.removeEventListener('resize', handleResize);
  }, []);

  return [windowSize, setWindowSize];
};

export default useWindowReszie;

[설명]
useWindowResize는 브라우저 창 사이즈가 변경될 때 width, height 값을 계산해 리턴하는 custom hook입니다.
하지만, 이 로직에서는 단 1px이라도 변하면 setState로 값이 변하게 되며 이로인해 useWindowResize를 사용하는 컴포넌트의 리렌더링이 발생합니다.

[문제점]

  • 너무 잦은 렌더링으로 인한 성능저하
  • SvgComponent와 동시에 사용 시 1px이라도 변화가 발생하면 리렌더가 발생하며 Suspense-Fallback으로 인해 화면이 깜빡거리는 현상 발생

[개선 방안]

  • lodash 사용 고려
@Bhanjo Bhanjo added the enhancement New feature or request label Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant