[React] 왜 Hook을 사용하는가

React Hook을 사용하는 이유

  1. Class-based component로 작성할 경우 state를 관리할때 코드가 여기저기 분산되지만 hook을 사용한다면 한군데로 코드가 모아지므로 가독성이 좋아진다. + Refactoring이 쉽다.
    • constructor에서는 state를 선언하고
    • componentDidMount()에서는 초기화를 하고
    • render()혹은 메소드에서 manipulation을 진행
  2. 간단한 component (자신의 state가 없고 props만을 사용한다거나)는 주로 functional component로 작성되는데, state를 추가하기 위해 class-based component로 바꾸지 않아도 된다.
  3. 헷갈리는 this를 사용할 필요가 없어진다.
  4. method binding이 필요가 없다.
  5. JS에서는 function이 더 가볍다. Class는 React에서는 불필요한 class의 메소드들을 추가적으로 포함한다.
  6. function은 closure를 사용한다. closure는 매우 memory-efficient하다.

Reference:
https://blog.bitsrc.io/why-we-switched-to-react-hooks-48798c42c7f
https://overreacted.io/how-are-function-components-different-from-classes/