[React] React Hook API (1)

2020. 6. 14. 10:36ใ†ETC/Programming

ํ•ด๋‹น ํฌ์ŠคํŒ…์—์„œ๋Š” React์— ๋‚ด์žฅ๋œ Hook API์— ๋Œ€ํ•ด  ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1๏ธโƒฃ useState

2๏ธโƒฃ useEffect


 

โš“๏ธ useState

ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ ์ƒํƒœ ๊ด€๋ฆฌ
state = {
	isLoading : true,
	recipeList : []
}

// AA๋ผ๋Š” list๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •.
this.setState({recipeList:AA, isLoading:false});

 ํด๋ž˜์Šค์—์„œ๋Š” state๋ฅผ ๊ฐ์ฒด๋กœ ๋ฌถ์–ด์„œ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

 

const [value, setValue] = useState(100);

useState๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  1. value : state ๊ฐ’
  2. setValue : state๋ฅผ ์„ค์ •ํ•˜๋Š” ํ•จ์ˆ˜
  3. value์˜ ์ดˆ๊ธฐ๊ฐ’์„ 100์œผ๋กœ ์„ค์ •.

(value, setValue๋Š” ์›ํ•˜๋Š” ์ด๋ฆ„์˜ ๋ณ€์ˆ˜๋กœ ์•„๋ฌด๋ ‡๊ฒŒ๋‚˜ ๋„ค์ด๋ฐํ•ด๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ, [A, setA] ๋ผ ๋„ค์ด๋ฐํ•˜๋Š” ๊ฒƒ์ด ์ฝ”๋”ฉํ•˜๋Š” ๋ฐ ํŽธํ•  ๊ฑฐ์˜ˆ์š”ใ€ฐ๏ธ)

 

setValue(0);
setValue(value-1);
  • setValue์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„ฃ์–ด์„œ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค
  • ์ „๋‹ฌ๋ฐ›์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ value๋กœ ๊ฐ’์ด ๋ฐ”๋€Œ๊ฒŒ ๋˜๊ณ  ์ปดํฌ๋„ŒํŠธ๋Š” ์ •์ƒ์ ์œผ๋กœ ๋ฆฌ๋ Œ๋”๋ง๋ฉ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ, useState๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”โ“

// useState๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉํ•˜๊ธฐ
setResult(value);
setFirst(Math.ceil(Math.random() * 9));
setSecond(Math.ceil(Math.random() * 9));
setValue('');

setState()๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค ๋ฆฌ๋ Œ๋”๋ง๋œ๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ, ์œ„์™€ ๊ฐ™์ด ํ•œ ๋ฒˆ์— useState๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด 4๋ฒˆ ๋ฆฌ๋ Œ๋”๋ง์ด ์ผ์–ด๋‚ ๊นŒ์š”?

์•„๋‹™๋‹ˆ๋‹ค. ๋ Œ๋”๋ง์€ ๋”ฑ ํ•œ ๋ฒˆ ์ผ์–ด๋‚˜๋Š”๋ฐ์š”, ์ด๊ฒƒ์„ ํ†ตํ•ด์„œ ๋น„๋™๊ธฐ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

 

 

 

โš“๏ธ useEffect

React Life Cycle(componentDidMount, componentDidUpdate, componentWillUnmount)์— ํ•ด๋‹นํ•˜๋Š” ๋กœ์ง์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
function useEffect(effect: EffectCallback, inputs?: InputIdentityList)
  • effect : renderํ•  ๋•Œ๋งˆ๋‹ค ๋งค๋ฒˆ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
  • input? : ํŠน์ • state๊ฐ€ ๋ณ€๊ฒฝ๋œ ๊ฒฝ์šฐ์—๋งŒ effect๊ฐ€ ์‹คํ–‰๋˜๊ฒŒ input์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

( ๋ฆฌ์•กํŠธ ๊ณต์‹ ๊ฐ€์ด๋“œ์— ๋”ฐ๋ฅด๋ฉด, ๋น„๋™๊ธฐ ์ž‘์—…์„ ํŒจ์นญํ•  ๋• useEffect ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์•ˆ์— ์ง์ ‘ ๋„ฃ์ง€ ๋ง๊ณ  ๊ทธ๊ฒƒ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ํ•˜๋„ค์š”ใ€ฐ๏ธ )

 

1๏ธโƒฃ Class์—์„œ componentDidMount()๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ

// API๋กœ๋ถ€ํ„ฐ data๋ฅผ fetchํ•˜๋Š” ๊ณผ์ •. ์™„๋ฃŒ ์ „๊นŒ์ง€๋Š” Loading...์„ ๋„์›Œ์คŒ.
class Data extends React.Component {
  constructor(props) {
    super(props);
    this.state = { item : null };
    this.setData = this.setData.bind(this);
  }

  componentDidMount() {
    API.getData()
      .then((response) => { this.setData(response) });
  }

  setData(data) {
    this.setState({ item: data });
  }
  
  render() {
    const isLoading = (this.state.item == null);
  	return { isLoading ? "Loading..."  : this.state.item }
  }
}

์‹คํ–‰ ์ˆœ์„œ : constructor → render → componentDidMount ( setData ) → render → componentDidUpdate

 

2๏ธโƒฃ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ useState()๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ

// useEffect๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„
import { useState, useEffect } from 'react';

export function Data() {
  const [data, setData] = useState(null);

  useEffect(() => {
    API.getData()
      .then((response) => { setData(response) });
  }, []);
  
  const isLoading = (data == null);
  return { isLoading ? "Loading..."  : data }
}

์œ„ ์ฝ”๋“œ์—์„œ useEffect์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ([])์ž…๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด ์ฒ˜์Œ ๋งˆ์šดํŠธ ์™„๋ฃŒ ํ›„(componentDidMount)์—๋งŒ useEffect๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ ๋‘๋ฒˆ์งธ ์ธ์ž๊ฐ€ ์•„์˜ˆ ์—†์—ˆ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”โ“๋ฌดํ•œ ๋ฐ˜๋ณต์œผ๋กœ ๋ฆฌ๋ Œ๋”๋ง๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ, useEffect๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋‘ ๋ฒˆ์งธ ์ธ์ž๋ฅผ ์œ ์˜ํ•ด์„œ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜์„ธ์š”!

 

๐Ÿ’ก clean-up

: effect๊ฐ€ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ฆฌ์•กํŠธ๋Š” ๊ทธ ํ•จ์ˆ˜๋ฅผ ์ •๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๋•Œ์— ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.

  • ๋ฆฌ์•กํŠธ๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ ํ•ด์ œ๋˜๋Š” ๋•Œ์— ์ •๋ฆฌ(clean-up)๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • ์ •๋ฆฌ(clean-up)๊ฐ€ ํ•„์š” ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ฐ˜ํ™˜์„ ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
const useBeforeLeave = (onBefore) => {
    const handle = (e) => {
        const {clientY} = e;
        if (clientY <= 0) //  ์œ„๋กœ ๊ฐ€๋ฉด ๋ฐœ์ƒ
            onBefore() ; 
    }

    useEffect(() => {
        document.addEventListener("mouseleave", handle);
        return () => document.removeEventListener("mouseleave", handle);
    }, []);
};

 

๐Ÿ’ก ํ•œ ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ effect๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค !

  • ์„œ๋กœ ๊ด€๋ จ ์—†๋Š” ๋กœ์ง์„ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฆฌ์•กํŠธ๋Š” ์ปดํฌ๋„ŒํŠธ์— ์‚ฌ์šฉ๋œ ๋ชจ๋“  effect๋ฅผ ์ง€์ •๋œ ์ˆœ์„œ์— ๋งž์ถฐ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

 

 

 

 

๐Ÿ”—  ์ฐธ๊ณ  ๋งํฌ

 

Hooks API Reference – React

A JavaScript library for building user interfaces

ko.reactjs.org

 

๋ฆฌ์•กํŠธ์˜ Hooks ์™„๋ฒฝ ์ •๋ณตํ•˜๊ธฐ

React Hooks ๋Š” v16.8 ์— ๋„์ž…๋œ ๊ฐœ๋…์œผ๋กœ์„œ, ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” useState, ๊ทธ๋ฆฌ๊ณ  ๋ Œ๋”๋ง ์งํ›„ ์ž‘์—…์„ ์„ค์ •ํ•˜๋Š” useEffect ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•˜์—ฌ ํ•œ๋ฒˆ ์ž์„ธํžˆ ๏ฟฝ

velog.io

 

React์˜ ์ƒˆ๋กœ์šด ํŒจ๋Ÿฌ๋‹ค์ž„, React Hooks

React Hooks์ด ์ƒ๊ธด ๋ฐฐ๊ฒฝ๊ณผ Basic Hook์ธ useState, useEffect์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ณ , custom hook์— ๊ด€ํ•ด ์‚ดํŽด๋ด…๋‹ˆ๋‹ค.

velog.io