Skip to content

Latest commit

ย 

History

History
178 lines (128 loc) ยท 5.04 KB

README.md

File metadata and controls

178 lines (128 loc) ยท 5.04 KB

React์˜ ์ƒํƒœ๊ด€๋ฆฌ

์ž‘์„ฑ์ž : ๊น€์šฐ์˜

React์˜ ์ƒํƒœ๊ด€๋ฆฌ ๊ธฐ๋ฒ•์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.

Reference

01. useState

1.1 ๊ธฐ๋ณธ์ ์ธ useState ๊ตฌ์กฐ

useState๋ฅผ ์‚ฌ์šฉํ•  ๋• ์ฝ”๋“œ์˜ ์ƒ๋‹จ์—์„œ import ๊ตฌ๋ฌธ์„ ํ†ตํ•ด ๋ถˆ๋Ÿฌ์˜ค๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

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

์ด๋Ÿฌํ•œ ๋ฌธ๋ฒ•์€ ๋ฐฐ์—ด ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น ๋ฌธ๋ฒ•์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋ฐฐ์—ด ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ์ค€๋น„ํ•œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

// array๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๊ณ  ์›์†Œ๊ฐ’์œผ๋กœ๋Š” ๊ฐ๊ฐ 'dog', 'cat', 'sheep'์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
const array = ['dog', 'cat', 'sheep'];
// ์•„๋ž˜ ์ฝ”๋“œ๋Š” ๊ฐ๊ฐ array์˜ 0๋ฒˆ์งธ, 1๋ฒˆ์งธ ์š”์†Œ๋ฅผ [first, second]์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
const [first, second] = array;
// ๊ฒฐ๋ก ์ ์œผ๋กœ first ๋ณ€์ˆ˜์—๋Š” 'dog', second ๋ณ€์ˆ˜์—๋Š” 'cat'์ด๋ผ๋Š” ๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
console.log(first, second); // dog cat

๋‹ค์Œ ์ฝ”๋“œ๋Š” useState์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๊ฐ€ ์ ์šฉ๋œ Component Counter.js์ž…๋‹ˆ๋‹ค.

/* 
    useState ๊ธฐ๋ณธ ๊ตฌ์กฐ
 */

import React, { useState } from 'react';

// Counter ํ•จ์ˆ˜ (ํ•˜๋‹จ์˜ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ• ๋•Œ๋งˆ๋‹ค ์‹คํ–‰๋จ)
const Counter = () => {
    // ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํƒœ๊ด€๋ฆฌ
    // useState(x) <- x์—๋Š” ์ดˆ๊ธฐ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋ฉฐ ์ด๋Š” value์— ๋ฐ˜์˜๋จ, setValue๋Š” value์˜ ์ƒํƒœ๋ฅผ updateํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜(?)
    const [value, setValue] = useState(0);
    // ์ดํ•ด๋ฅผ ์œ„ํ•ด `console.log` ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•ด๋ณด๋ฉด value๋Š” ์„ค์ •๊ฐ’, setValue๋Š” ํ•จ์ˆ˜๋กœ ์ถœ๋ ฅ๋œ๋‹ค.
    console.log(value, setValue);
  return (
    <div>
      <p>
        ํ˜„์žฌ ์นด์šดํ„ฐ ๊ฐ’์€ <b>{value}</b> ์ž…๋‹ˆ๋‹ค.
      </p>
      <button onClick={() => setValue(value + 1)}>+1</button>
      <button onClick={() => setValue(value - 1)}>-1</button>
    </div>
  );
};

export default Counter;

์œ„ Component๋ฅผ App.js์—์„œ ํ˜ธ์ถœํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

+1 ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๊ฒŒ๋˜๋ฉด setValue ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ํ˜„์žฌ value์— 1์„ ๋”ํ•˜์—ฌ ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธ,

-1๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๊ฒŒ๋˜๋ฉด ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ setValueํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ˜„์žฌ value์— 1์„ ๋นผ๊ณ  ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.

1.2 ์ค‘๋ณต ์‚ฌ์šฉ useState ๊ตฌ์กฐ

useState๋Š” ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ๋™์‹œ์— ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ Info.js Component์ž…๋‹ˆ๋‹ค.

/* 
    useState ์ค‘์ ‘ (์—ฌ๋Ÿฌ๊ฐœ ์‚ฌ์šฉ)
 */
import React, { useState } from 'react';

const Info = () => {
    // ์ด๋ฆ„์„ ์ €์žฅํ•  name state
    const [name, setName] = useState('');
    // ๋‹‰๋„ค์ž„์„ ์ €์žฅํ•  nickname state
    const [nickname, setNickname] = useState('');

    // ์ด๋ฆ„์ด ๋ณ€๊ฒฝ๋˜๋ฉด ์‹คํ–‰๋  onChangeName ํ•จ์ˆ˜
    const onChangeName = e => {
        setName(e.target.value);
    };

    // ๋‹‰๋„ค์ž„์ด ๋ณ€๊ฒฝ๋˜๋ฉด ์‹คํ–‰๋  onChangeNickname ํ•จ์ˆ˜
    const onChangeNickname = e => {
        setNickname(e.target.value);
    };

    return (
        <div>
            <div>
            {/* input์˜ value๋Š” name state๋กœ ์ง€์ •, ๊ฐ’์ด ๋ณ€๊ฒฝ๋ ๋•Œ onChangeName ํ•จ์ˆ˜ ํ˜ธ์ถœ */}
            <input value={name} onChange={onChangeName} />
            {/* input์˜ value๋Š” nickname state๋กœ ์ง€์ •, ๊ฐ’์ด ๋ณ€๊ฒฝ๋ ๋•Œ onChangeNickname ํ•จ์ˆ˜ ํ˜ธ์ถœ */}
            <input value={nickname} onChange={onChangeNickname} />
        </div>
        <div>
            <div>
            <b>์ด๋ฆ„:</b> {name}
            </div>
            <div>
            <b>๋‹‰๋„ค์ž„: </b>
            {nickname}
            </div>
        </div>
        </div>
    );
};

export default Info;

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ App.js์—์„œ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

02. useEffect

useEffect๋Š” ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง ๋  ๋•Œ๋งˆ๋‹ค ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ์„ค์ • ํ•  ์ˆ˜ ์žˆ๋Š” Hook ์ž…๋‹ˆ๋‹ค.

ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์˜ componentDidMount์™€ componentDidUpdate๋ฅผ ํ•ฉ์นœ ํ˜•ํƒœ๋กœ ๋ณด์•„๋„ ๋ฌด๋ฐฉํ•ฉ๋‹ˆ๋‹ค.

์œ„์—์„œ ๋งŒ๋“ค์—ˆ๋˜ Info component์— useEffect๋ฅผ ์ ์šฉํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

info.js ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.

import React, { useState, useEffect } from 'react';

const Info = () => {
  const [name, setName] = useState('');
  const [nickname, setNickname] = useState('');
  useEffect(() => {
    console.log('๋ Œ๋”๋ง์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!');
    console.log({
      name,
      nickname
    });
  });

  const onChangeName = e => {
    setName(e.target.value);
  };

  const onChangeNickname = e => {
    setNickname(e.target.value);
  };

  return (
    (...)
  );
};

export default Info;

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ App.js์—์„œ ์ˆ˜์ •ํ•œ Info-useEffect ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค.