๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ฑ Cross Platform/React Native

[React Native] Hooks๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ  (feat. useState,useEffect)

by Fomagran ๐Ÿ’ป 2022. 6. 18.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์š”์ฆ˜ React Native๋ฅผ ๊ณต๋ถ€ํ•˜๋Š”๋ฐ useState,useEffect๊ฐ€ ์ •๋ง ๋งŽ์ด ๋ณด์ด๋”๋ผ๊ตฌ์š”.

 

์ด๊ฒŒ ๋ญ”์ง€ ๊ทธ๋ฆฌ๊ณ  ์™œ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€ ์ •ํ™•ํžˆ ์•Œ๊ณ ์ž ์ด ๊ธ€์„ ์ •๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~

 

์–ธ์–ด๋Š” TypeScript, ํ™˜๊ฒฝ์€ Expo๋กœ ์ง„ํ–‰ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!


Hooks

 

Hooks์— ๋Œ€ํ•ด ์ •๋ง ์ •๋ง ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…ํ•ด ๋“œ๋ฆด๊ฒŒ์š”.

 

(์™œ๋ƒํ•˜๋ฉด useState,useEffect๊ฐ€ ๋ชจ๋‘Hooks์ด๊ธฐ ๋–„๋ฌธ์ด์ฃ .)

 

๊ธฐ์กด์˜ React Native์—์„  ์ƒํƒœ ๊ด€๋ฆฌ์™€ ๋ผ์ดํ”„ ์‚ฌ์ดํด์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ Class ํ˜•ํƒœ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

์ด์™€ ๊ฐ™์ด Class๋กœ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋ฉด ์ฝ”๋“œ๋ฅผ ์žฌ์‚ฌ์šฉ ํ•˜๊ธฐ๊ฐ€ ํž˜๋“ค๊ณ , ๋ผ์ดํ”„ ์‚ฌ์ดํด ๋ฉ”์„œ๋“œ๊ฐ€ ์ถ”๊ฐ€ ๋˜๋ฉด์„œ ์“ธ ๋ฐ ์—†์ด ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด(์ฝ”๋“œ๋ฅผ ์žฌ์‚ฌ์šฉ ํ•˜๊ณ  ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด) ํƒ„์ƒํ•œ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.


๊ธฐ์กด Class์˜ ๋ฌธ์ œ์  ์ฝ”๋“œ๋กœ ์‚ดํŽด๋ณด๊ธฐ

 

์•„๋ž˜์™€ ๊ฐ™์ด Increase ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ˆซ์ž๊ฐ€ 1 ์ฆ๊ฐ€ํ•˜๊ณ , Decrease ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ˆซ์ž๊ฐ€ 1 ๊ฐ์†Œํ•˜๋Š” ํ™”๋ฉด์„ ๋งŒ๋“ ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

1. state๋ฅผ ๋งŒ๋“ค์–ด์ค˜์•ผ ํ•œ๋‹ค.

 

๊ฐ€์žฅ ๋จผ์ € ์ € ์ˆซ์ž์˜ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ผ state๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๊ณ  ์ดˆ๊ธฐ๊ฐ’์„ ์ง€์ •ํ•ด ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

import React from "react";

export default class CounterComponent extends React.Component<
  {},
  { count: number }
> {
  state = {
    count: 0,
  };
}

 

2. ํ•ด๋‹น ๊ธฐ๋Šฅ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์–ด ์ค˜์•ผ ํ•œ๋‹ค.

 

ํ™”๋ฉด์„ ๋ Œ๋”๋ง ํ•œ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

  render() {
    return (
      <div>
        <div style={{ fontSize: "100px" }}>{this.state.count}</div>
        <div></div>
        <button onClick={this.increaseNumber}>Increase</button>
        <button onClick={this.decreaseNumber}>Decrease</button>
      </div>
    );
  }

 

onClick์— ์ง์ ‘ ๊ธฐ๋Šฅ์„ ์ž‘์„ฑํ•  ์ˆ˜๊ฐ€ ์—†์–ด ๋”ฐ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

  increaseNumber = () => {
    this.setState((state) => ({
      count: state.count + 1,
    }));
  };

  decreaseNumber = () => {
    this.setState((state) => ({
      count: state.count - 1,
    }));
  };

 

3. Life Cycle์— ๋”ฐ๋ผ setState ํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ์ž‘์„ฑํ•ด ์ค˜์•ผ ํ•œ๋‹ค.

 

state์— ํ•ด๋‹น ๋ฌธ์žฅ์„ ๋‚˜ํƒ€๋‚ผ title์„ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

 

  state = {
    count: 0,
    title: "",
  };

 

component๊ฐ€ mount ๋˜์—ˆ์„ ๋•Œ์™€ update ๋˜์—ˆ์„ ๋•Œ setState๋ฅผ ๋”ฐ๋กœ ์ž‘์„ฑํ•ด ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

  componentDidMount() {
    setTimeout(() => {
      this.setState({
        title: `Initial count is ${this.state.count}`,
      });
    }, 1000);
  }

  componentDidUpdate() {
    setTimeout(() => {
      this.setState({
        title: `Updated count is ${this.state.count}`,
      });
    }, 1000);
  }

Hooks๋กœ ์œ„ ๋ฌธ์ œ ๊ฐœ์„ ํ•˜๊ธฐ

 

๋จผ์ € Component๋ฅผ class ํ˜•ํƒœ๊ฐ€ ์•„๋‹Œ function ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

 

1. useState

 

๋จผ์ € Hooks์—์„œ ์ œ๊ณตํ•˜๋Š” useState๋ฅผ ์‚ฌ์šฉํ•ด ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ• ๊ฑด๋ฐ์š”.

 

๊ทœ์น™์€ [์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ผ ๋ณ€์ˆ˜, ์ƒํƒœ๋ฅผ ๋ฐ”๊ฟ€ ํ•จ์ˆ˜] = useState(๊ธฐ๋ณธ๊ฐ’) ์œผ๋กœ ์ž‘์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

 

export default function CounterComponent() {
  const [count, setCount] = useState(0);

}

 

2. onClick ๋ฉ”์„œ๋“œ์— ๋ฐ”๋กœ ์ ์šฉํ•˜๊ธฐ

 

์œ„์—์„œ setCount๊ฐ€ ์ƒํƒœ๋ฅผ ๋ฐ”๊ฟ€ ํ•จ์ˆ˜๋ผ๊ณ  ์„ค๋ช… ๋“œ๋ ธ์ฃ ?

 

onClick์„ ํ–ˆ์„ ๋•Œ setCount๋กœ count๋ฅผ +1 ๋˜๋Š” -1 ํ•˜์—ฌ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 return (
    <div>
      <div style={{ fontSize: "100px" }}>{count}</div>
      <div></div>
      <button onClick={() => setCount(count + 1)}>Increase</button>
      <button onClick={() => setCount(count - 1)}>Decrease</button>
    </div>
  );

 

 

3. useEffect

 

์œ„ class๋กœ ๊ตฌํ˜„ ํ–ˆ์„ ๋•Œ ๋ผ์ดํ”„ ์‚ฌ์ดํด ๋ฉ”์„œ๋“œ ๋งˆ๋‹ค setState๋ฅผ ๊ฐ ์ž‘์„ฑํ•ด ์ค˜์•ผ ํ–ˆ๋Š”๋ฐ์š”.

 

useEffect๋ฅผ ์“ฐ๋ฉด ์ฝ”๋“œ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ค„์—ฌ์ค„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ Œ๋”๋ง ์‹œ ๋ฐœ์ƒํ•˜๋Š” side effect๋„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์ด 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

useEffect(() => {})

 

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด ํ™”๋ฉด์ด ๋ Œ๋”๋ง ๋  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

 

๊ณ ๋กœ ์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๊ฑฐ์˜ ์—†์Šต๋‹ˆ๋‹ค.

 

useEffect(() => {},[])

 

์ด์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉด ๋ Œ๋”๋ง ํ›„ ๋”ฑ ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

 

useEffect(() => {},[์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜)

 

๋ Œ๋”๋ง ํ›„ ํ•œ๋ฒˆ ์‹คํ–‰๋˜๊ณ , ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

 

๋˜‘๊ฐ™์ด title์„ useState๋กœ ๋งŒ๋“ค์–ด ์ฃผ๊ณ ,

 

  const [title, setTitle] = useState(`Initial count is ${count}`);

 

useEffect๋ฅผ ๋‹จ ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋„๋ก useEffect(() => {},[]) ๊ฐ™์ด ์ž‘์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

 

์ค‘๊ด„ํ˜ธ ์‚ฌ์ด์—” setTitle์„ ์ž‘์„ฑํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

  useEffect(() => {
    setTitle(`Updated count is ${count}`);
  }, []);

์ฝ”๋“œ๋Ÿ‰ ๋น„๊ต

 

์™ผ์ชฝ์ด Hooks๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ ์˜ค๋ฅธ์ชฝ์ด class๋กœ ๊ตฌํ˜„ํ•œ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.

 

์•„์ฃผ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์ž„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ฝ”๋“œ ๊ธธ์ด๊ฐ€ ์•ฝ 2๋ฐฐ ์ฐจ์ด๊ฐ€ ๋‚˜๋Š” ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด๊ฒƒ์„ ํ†ตํ•ด์„œ class๋กœ ๊ตฌํ˜„ํ–ˆ์„ ๋•Œ์™€ ๋น„๊ตํ•˜์—ฌ Hooks๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„ํ–ˆ์„ ๋•Œ์˜ ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง€๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ํŽธ๋ฆฌํ•˜๋‹ค๋Š” ๊ฑธ ๋Š๊ผˆ์Šต๋‹ˆ๋‹ค.

 


Hooks๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ 

 

1. ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

2. ์ฝ”๋“œ๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

3. ์‚ฌ์ด๋“œ ์ดํŽ™ํŠธ๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

4. Class ์—†์ด ๋ฆฌ์•กํŠธ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


Reference

 

 

Hook์˜ ๊ฐœ์š” – React

A JavaScript library for building user interfaces

ko.reactjs.org

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€