How Are Function Components Different from Classes?

hooks가 나오기 전 한동안은 클래스가 더 많은 기능에 대한 엑세스를 제공한다는 것이 차이점 이었지만 더 이상은 아니다.

이 글에서 둘 사이의 가장 큰 차이점을 살펴볼 것이다.

함수형 컴포넌트가 2015년에 도입 된 이래로 존재 했지만, 종종 간과되었다.

함수형 컴포넌트는 렌더링 된 값을 캡처 한다.

이것이 무엇을 의미하는가

function ProfilePage(props) {
	const showMessage = () => {
		alert('Followed ' + props.user);
	}

	const handleClick = () => {
		setTimeout(showMessage, 3000);
	}

	return (
		<button onClick={handleClick}>Follow</button>
	);
}

이것을 클래스로 작성해 보자

class ProfilePage extends React.Component {
  showMessage = () => {
    alert('Followed ' + this.props.user);
  };

  handleClick = () => {
    setTimeout(this.showMessage, 3000);
  };

  render() {
    return <button onClick={this.handleClick}>Follow</button>;
  }
}

계속하기 전 이 글의 차이점은 React Hooks와 아무런 관련이 없다. 이것은 React fuction과 class의 차이점 이다.