ReactJS 디자인 패턴 ~State 동시 배치~

발행: (2026년 1월 5일 오후 09:06 GMT+9)
1 min read
원문: Dev.to

Source: Dev.to

Collocating State 개요

Collocating State는 상태를 사용되는 위치에 가능한 한 가깝게 배치하는 방법입니다. 이 패턴은 복잡성을 줄이고 컴포넌트를 이해하기 쉽게 만들어 줍니다.

이전

App 컴포넌트

function App() {
  const [isVisible, setIsVisible] = useState(false);
  return (
    
      
    
  );
}

ToggleSection 컴포넌트

function ToggleSection({ isVisible, setIsVisible }) {
  return (
    
       setIsVisible(!isVisible)}>
        {isVisible ? "Hide" : "Show"} Content
      
      {isVisible && (
        
          
This content can be toggled!

        
      )}
    
  );
}

이후

App 컴포넌트 (상태 제거)

function App() {
  // Removed the isVisible state.
  return (
    
      
    
  );
}

ToggleSection 컴포넌트 (상태 공동 배치)

function ToggleSection() {
  // Placed the isVisible state here.
  const [isVisible, setIsVisible] = useState(false);
  return (
    
       setIsVisible(!isVisible)}>
        {isVisible ? "Hide" : "Show"} Content
      
      {isVisible && (
        
          
This content can be toggled!

        
      )}
    
  );
}
Back to Blog

관련 글

더 보기 »

State를 이동하여 리렌더링 최적화

소개 성능을 향상시키고 불필요한 재‑렌더링을 줄이는 한 가지 방법은 상태를 낮추는 것입니다. 특히 그 상태가 특정 부분에만 영향을 미치는 경우에 효과적입니다.