๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/React Native

[React Native] State์™€ React Hook(useState) - ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ ๋™์ ์ธ ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๊ธฐ

by Jay Din 2023. 6. 15.
728x90
๋ฐ˜์‘ํ˜•
๋ฐ˜์‘ํ˜•

State

ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ ๋™์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋• state๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.(๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜๋ผ๊ณ  ์ƒ๊ฐ)

setState

state์— ์žˆ๋Š” ๊ฐ’์„ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฌด์กฐ๊ฑด this.setState๋ฅผ ๊ฑฐ์ณ์•ผํ•œ๋‹ค.

๋ฆฌ์•กํŠธ์—์„  ์ด ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง ๋˜๋„๋ก ์„ค๊ณ„๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

setState๋Š” ๊ฐ์ฒด๋กœ ์ „๋‹ฌ๋˜๋Š” ๊ฐ’๋งŒ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค

state๋ฅผ ์ด์šฉํ•œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ˆซ์ž(number)๊ฐ€ ์นด์šดํŠธ ๋˜๋Š” ์ฝ”๋“œ

import React, { Component } from 'react';
import {View, Text, Button} from 'react-native';
class App extends Component {
  state = {
    number: 0
  };
  up = () => {
    this.setState({
      number: this.state.number + 1
    });
  };
  down = () => {
    this.setState({
      number: this.state.number - 1
    });
  };
  render() {
    return (
      <View>
        <Text style={{fontSize:30}}>์นด์šดํ„ฐ</Text>
        <Text style={{fontSize:30}}>๊ฐ’:
               {this.state.number}</Text>
        <Button onPress={this.up} title="+">
                </Button>
        <Button onPress={this.down} title="-"> 
                </Button>
      </View>
    );}}
export default App;

up๊ณผ down์— ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์“ฐ๋Š” ์ด์œ , - this๋ฅผ ์ž๋™์œผ๋กœ ๋ฐ”์ธ๋”ฉ ํ•ด์ค€๋‹ค.

JS์—์„œ ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐ”์ธ๋”ฉ ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค.

this.up์„ ๋ฐ”์ธ๋”ฉ ํ•˜์ง€ ์•Š๊ณ  onPress์— ์ „๋‹ฌํ•˜์˜€๋‹ค๋ฉด, ํ•จ์ˆ˜๊ฐ€ ์‹ค์ œ๋กœ ํ˜ธ์ถœ๋  ๋•Œ this๋Š” undefined ๋œ๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ onPress={this.up}๊ณผ ๊ฐ™์ด ๋’ค์— ()๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•  ๊ฒฝ์šฐ, this.down.bind(this)์œผ๋กœ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•ด์•ผํ•œ๋‹ค.

ํ•˜์ง€๋งŒ, ํ™”์‚ดํ‘œํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด this๋Š” ์ƒ์œ„ ํƒœ๊ทธ๋ฅผ ๊ฐ€๋ฅดํ‚ค๊ธฐ ๋•Œ๋ฌธ์— bindํ•˜์ง€ ์•Š์•„๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

โ€‹

๋ฐ”์ธ๋”ฉ(React Binding)์€ ๋ฆฌ์•กํŠธ์—์„œ ์ปดํฌ๋„ŒํŠธ์— ์ด๋ฒคํŠธ ๋ฉ”์„œ๋“œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋กœ ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉํ•˜๋Š” this์˜ ๋Œ€์ƒ์„ ์ง€์ •ํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

โ€‹

renderํ•จ์ˆ˜ ์•ˆ์—์„œ this๋Š” ์ปดํฌ๋„ŒํŠธ ์ž์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค์ง€๋งŒ ์ด๋ฒคํŠธ (onPress)ํ•จ์ˆ˜ ์•ˆ์—์„œ this๋Š” ์•„๋ฌด๊ฒƒ๋„ ๊ฐ€๋ฅดํ‚ค์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— onPressํ•จ์ˆ˜๋ฅผ bind๋กœ this๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

React Hook

Hook์€ ๋ฆฌ์•กํŠธ v16.8์— ์ƒˆ๋กœ ๋„์ž…๋œ ๊ธฐ๋Šฅ์ด๋‹ค. ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ์ˆ ์„ Hook์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

โ€‹์ฆ‰, Hook์€ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ React state(์ƒํƒœ๋ณ€ํ™”)์™€ ์ƒ๋ช…์ฃผ๊ธฐ ๊ธฐ๋Šฅ(lifecycle features)์„ “์—ฐ๋™(hook into)”ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค.

โ€‹

๊ธฐ์กด์—๋Š” ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋‹ค๊ฐ€ state๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์œผ๋ฉด ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ”๊พธ๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ์—ˆ๋‹ค.

Hook์€ ๋งŒ๋“ค์–ด์ง„ ๋ชฉ์  ์ž์ฒด๊ฐ€ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ์ด๊ธฐ ๋•Œ๋ฌธ์— ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ๋Š” ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ๋‹จ์ 

Logic์˜ ์žฌ์‚ฌ์šฉ์ด ์–ด๋ ต๋‹ค.

ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์ปดํฌ๋„ŒํŠธ ์ž์ฒด๋ฅผ ์žฌ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ปดํฌ๋„ŒํŠธ ํด๋ž˜์Šค ๋ถ€๋ถ„์ ์ธ API ์‚ฌ์šฉ ๋ฐ state๋ฅผ ๋‹ค๋ฃจ๋Š” ๋“ฑ์˜ ์ฒ˜๋ฆฌ์—๋Š” ์žฌ์‚ฌ์šฉ์— ์ œ์•ฝ์ด ๋”ฐ๋ฅธ๋‹ค.

โ€‹

์ฝ”๋“œ๊ฐ€ ๊ธธ๊ณ  ๋ณต์žกํ•ด์ง„๋‹ค.

๊ธฐ๋ณธ ๊ทœ์น™์„ ๋”ฐ๋ผ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•˜๊ณ  ๊ธธ๋‹ค

โ€‹

Hook ์ฒ˜๋ฆฌ์‹œ ์žฅ์ 

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

ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ๋ณต์žก์„ฑ, ์žฌ์‚ฌ์šฉ์„ฑ์˜ ๋‹จ์ ๋“ค์ด ํ•ด๊ฒฐ๋œ๋‹ค.

 

๊ธฐ๋ณธ Hook์ธ useState

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

ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ๋ฆฌ์•กํŠธ์—์„œ ์ž์ฒด์ ์œผ๋กœ ์ง€์›ํ•˜๋Š” ‘useState’๋ผ๋Š” React hooks๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ˜•์‹

// import ํ•˜๊ธฐ
import React, {useState} from "react";
// useState ์„ ์–ธ
const [<์ƒํƒœ ๊ฐ’ ์ €์žฅ ๋ณ€์ˆ˜>, <์ƒํƒœ ๊ฐ’ ๊ฐฑ์‹  ํ•จ์ˆ˜>] = useState(<์ƒํƒœ ์ดˆ๊ธฐ ๊ฐ’>);
๋ฐ˜์‘ํ˜•

 

useState๋ฅผ ์ด์šฉํ•œ  ์ˆซ์ž(number) ์นด์šดํŠธ ์˜ˆ์ œ

import React, {useState} from "react";
import {View, Text, Button} from 'react-native';

function App(){
  const [number, setNumber] = useState(0)

  const up = () =>{
    setNumber(number+1)
  }
  const down = () =>{
    setNumber(number-1)
  }
  return(
    <View>
      <Text style={{fontSize:30}}>์นด์šดํ„ฐ</Text>
      <Text style={{fontSize:30}}>๊ฐ’: {number}</Text>
      <Button title="+" onPress={up} />
      <Button title="-" onPress={down} />
    </View>
  );
}
export default App;

์‹คํ–‰ํ™”๋ฉด

 

Hook API

ํ›…
๋ชฉ์ 
๊ธฐ๋ณธ ํ›…
useState
์ƒํƒœ ์œ ์ง€ ๋ฐ ๋ณ€๊ฒฝ
useEffect
์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ
useContext
์ •๋ณด ๊ณต์œ 
์ถ”๊ฐ€ ํ›…
useReducer
์ƒํƒœ ์œ ์ง€ ๋ฐ ๋ณ€๊ฒฝ
(Redux)
useCallback
์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์บ์‹œ
useMemo
๋ฐ์ดํ„ฐ, ๊ฐ’ ์บ์‹œ
useRef
์ปดํฌ๋„ŒํŠธ๋‚˜ HTML ์š”์†Œ๋ฅผ ๋ž˜ํผ๋Ÿฐ์Šค๋กœ ๊ด€๋ฆฌ
728x90
๋ฐ˜์‘ํ˜•