횡단 관심사(Cross-Cutting concerns)

Untitled

HOC 만드는 법

  1. 함수 with이름(InputComponent, 그밖에 인자들...)작성한다.
  2. 함수 내에서 새로운 컴포넌트(OuputComponent)를 생성한다.

예제

마운트 시 이벤트 발동

function withMountEvent(InputComponent, componentName){ // 필요한 매개변수는 추가하기
  return (
    // 반환 컴포넌트 작성
    class OutputCompnent extends React.Component {
      // 마운트 라이프 사이클에서 이벤트 작성
      componentDidMount() {
        sendEvent(componentName);
      }
      render(){
        return (
          <InputComponent {...this.props} /> // InputComponent를 OuputComponent에서 반환
        )
      }
    }
  )
}

마운트 여부를 알려주는 HOC