[React] React Hooks - Component & Hook

Hannahhhยท2022๋…„ 9์›” 28์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
23/30

๐Ÿ” Component & Hook


React Hooks๋Š” React 16.8 ๋ฒ„์ „๋ถ€ํ„ฐ ์ถ”๊ฐ€๋œ ๊ธฐ๋Šฅ์œผ๋กœ, ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์™€ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž‘์—…์„ ํ•˜๋˜ ๊ธฐ์กด ๋ฐฉ์‹์—์„œ ๋ฒ—์–ด๋‚˜ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ ๋” ์ง๊ด€์ ์ธ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“  ๊ธฐ๋Šฅ์ด๋‹ค.

๋›ฐ์–ด๋‚œ ์žฌ์‚ฌ์šฉ์„ฑ๊ณผ ์ง๊ด€์„ฑ์„ ๊ฐ–๋Š” ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ Hook์€ ์‚ฌ์šฉ๋ฅ ์ด ๋งค์šฐ ๋†’์€ ํŽธ์ด๋‹ค.



๐Ÿ‘€ Class Component


ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ ์ด์ „์— ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐฉ์‹์œผ๋กœ, ๋ณต์žกํ•ด์งˆ ์ˆ˜๋ก ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ต๊ณ , state ๋กœ์ง์„ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์–ด๋ ต๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

์ด๋Ÿฌํ•œ ๋ฌธ์ œ ๋•Œ๋ฌธ์— ์ ์ง„์ ์œผ๋กœ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋„˜์–ด๊ฐ€๊ฒŒ ๋˜์—ˆ์œผ๋‚˜, ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋Š” state๊ฐ’์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ๋“ค์ด class ์ปดํฌ๋„ŒํŠธ์— ๋น„ํ•ด ๋ฏธ์ง„ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•œ ๋ณด์™„์„ ์œ„ํ•ด Hook์ด ๋„์ž…๋˜์—ˆ๋‹ค.


Class Component -> Function Component -> Function Component + Hook


์•„๋ž˜๋Š” <Counter /> ์ปดํฌ๋„ŒํŠธ๋ฅผ Class๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋กœ, ๋‹จ์ˆœํžˆ ์นด์šดํŒ…์„ ํ•˜๋Š” ๊ธฐ๋Šฅ๋งŒ ๊ตฌํ˜„๋˜์–ด์žˆ๋‹ค.


class ClassCount extends React.Component {
	constructor(props) {
		super(props);
		this.state = {
			count: 0
		};
	}

render() {
	return (
		<div>
			<p>count: {this.state.count} per times clicked</p>
			<button onClick={() => this.setState({ count: this.state.count + 1 })}>
				Click this button
			</button>
		</div>
		);
	}
}




๐Ÿ‘€ Function Component


์•„๋ž˜๋Š” Class Component ์˜ˆ์‹œ ์ฝ”๋“œ์™€ ๊ฐ™์€ ๋™์ž‘์„ ํ•˜๋ฉฐ,<counter />์ปดํฌ๋„ŒํŠธ๋ฅผ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ์ด๋‹ค.

Class Component์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ, ์ƒ๋Œ€์ ์œผ๋กœ ์ง๊ด€์ ์ด๊ณ  ๋ณด๊ธฐ ์‰ฝ๋‹ค.


const FunctionalCount = () => {
	const [count, setCount] = React.useState(0);
	return (
		<div>
			<p>count: {count}</p>
			<button onClick={() => setCount(count + 1)}>Click this button</button>
		</div>
	);
};

useState()๋ผ๋Š” ์ƒํƒœ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” State Hook์„ ํ˜ธ์ถœํ•ด, ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์•ˆ์— state๋ฅผ ์ถ”๊ฐ€ํ•œ ํ˜•ํƒœ์ด๋‹ค.

์ด๋Ÿฌํ•œ State Hook์€ ์—ฌ๋Ÿฌ ๊ฐœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง ๋˜๋”๋ผ๋„ state๋Š” ๊ทธ๋Œ€๋กœ ์œ ์ง€๋œ๋‹ค.




๐Ÿ‘€ Hook


ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ state ๊ฐ’ ๋ฐ ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ํŽธํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ๋กœ, function์œผ๋กœ๋งŒ React๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— Class Component์—์„œ๋Š” ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.



โœ” Hook ์‚ฌ์šฉ ๊ทœ์น™


1. React ํ•จ์ˆ˜์˜ ์ตœ์ƒ์œ„์—์„œ๋งŒ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.

๋ฐ˜๋ณต๋ฌธ, ์กฐ๊ฑด๋ฌธ, ์ค‘์ฒฉ๋œ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์‹คํ–‰ ์‹œ, ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.

2. React ํ•จ์ˆ˜ ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•œ๋‹ค.

์˜ค์ง React Function Componet๋‚˜ Custom Hook์—์„œ๋งŒ ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•˜๋‹ค.(์ผ๋ฐ˜ JavaScript ํ•จ์ˆ˜xx)




Reference:

์ฝ”๋“œ์Šคํ…Œ์ด์ธ 

https://www.xenonstack.com/blog/functional-vs-class-components

0๊ฐœ์˜ ๋Œ“๊ธ€