ํ”„๋ก ํŠธ์—”๋“œ/React

ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ

alswlfl 2022. 12. 2. 14:59

→ ํ˜„์žฌ๋Š” ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ๋‚˜์ค‘์— ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ฑฐ๋‚˜, ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ+Hooks๋กœ ๋ชปํ•˜๋Š” ์ž‘์—…์˜ ๊ฒฝ์šฐ ๋“ฑ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ๋ฅผ ์จ์•ผํ•˜๋Š” ์ƒํ™ฉ ์กด์žฌ

<๊ธฐ๋ณธ ํ˜•์‹>

import React, {Component} from 'react';

class App extends Component{//classํ‚ค์›Œ๋“œ ํ•„์š”, Component๋กœ ์ƒ์† ๋ฐ›์•„์•ผํ•จ
	state={//constructor์—†์ด ๋ฐ”๋กœ state์ดˆ๊ธฐ๊ฐ’ ์„ค์ • ๊ฐ€๋Šฅ, ๊ฐ์ฒด ํ˜•์‹monsters:[],
        userInput:"",
    };
    onClick={()=>{//this.setState ํ•จ์ˆ˜๋กœ state ๊ฐ’ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅthis.setState({number:number+1});
    }}
	render(){//render()๋ฉ”์†Œ๋“œ ํ•„์š”const name='react';
        return <div className="react">{name}</div>
   	}
}

<ํ•จ์ˆ˜ํ˜• vs ํด๋ž˜์Šคํ˜•>

  • ํ•จ์ˆ˜ํ˜• ์ฝ”๋“œ
import React from 'react';

function Hello({ color, name, isSpecial }) {
  return (
    <div style={{ color }}>
      {isSpecial && <b>*</b>}
      ์•ˆ๋…•ํ•˜์„ธ์š” {name}
    </div>
  );
}

Hello.defaultProps = {
  name: '์ด๋ฆ„์—†์Œ'
};

export default Hello;
  • ํด๋ž˜์Šคํ˜• ์ฝ”๋“œ
import React, { Component } from "react";

class Hello extends Component {
  static defaultProps = {
    name: "์ด๋ฆ„์—†์Œ",
  };

  render() {
    const { color, name, isSpecial } = this.props;
    return (
      <div style={{ color }}>
        {isSpecial && <b>*</b>}
        ์•ˆ๋…•ํ•˜์„ธ์š” {name}
      </div>
    );
  }
}

export default Hello;

[ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ ํŠน์ง•]

  1. render() ๋ฉ”์„œ๋“œ ๊ผญ ์กด์žฌํ•ด์•ผํ•จ (ํ•ด๋‹น ๋ฉ”์„œ๋“œ์—์„œ ๋ Œ๋”๋งํ•˜๊ณ  ์‹ถ์€ JSX ๋ฐ˜ํ™˜)
  2. props ์กฐํšŒ ํ•  ๋•Œ, this.props ๋กœ ์กฐํšŒ
  3. defaultProps๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์€ ํ•จ์ˆ˜ํ˜•๊ณผ ๋˜‘๊ฐ™์ด ํ•˜๊ฑฐ๋‚˜, ํด๋ž˜์Šค ๋‚ด๋ถ€์— static ํ‚ค์›Œ๋“œ์™€ ํ•จ๊ป˜ ์„ ์–ธ ๊ฐ€๋Šฅ
  4. ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ ์ปค์Šคํ…€ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค ๋•Œ, renderํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์„ ์–ธ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ ํด๋ž˜์Šค ์•ˆ์— ์ปค์Šคํ…€ ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธ
    1. ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์ข…์†๋œ ํ•จ์ˆ˜ → “๋ฉ”์„œ๋“œ”
    2. ํด๋ž˜์Šค์—์„œ ์ปค์Šคํ…€ ๋ฉ”์„œ๋“œ ๋งŒ๋“ค ๋•Œ ๋ณดํ†ต ์ด๋ฆ„์„ handle…์ด๋ผ๊ณ  ์ง€์Œ(๊ผญ ์ง€ํ‚ฌ ํ•„์š”๋Š” ์—†์Œ)

์ปค์Šคํ…€ ๋ฉ”์„œ๋“œ ๋งŒ๋“ค๊ธฐ

import React, { Component } from "react";

class Counter extends Component {
  handleIncrease() {
    console.log("increase");
  }
  handleDecrease() {
    console.log("decrease");
  }
  render() {
    return (
      <div>
        <h1>0</h1>
        <button onClick={this.handleIncrease}>+1</button>
        <button onClick={this.handleDecrease}>-1</button>
      </div>
    );
  }
}

export default Counter;

โ—๏ธthis๋Š” ์ปดํฌ๋„ŒํŠธ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฅด์ผœ์•ผ ํ•˜๋Š”๋ฐ, ํ˜„์žฌ ๊ตฌํ˜„ํ•œ ๋ฉ”์„œ๋“œ์—์„œ this๋ฅผ ์กฐํšŒํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฅดํ‚ค์ง€ ์•Š๊ฒŒ๋จ

→ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ๋ฉ”์„œ๋“œ๋“ค์„ ์ด๋ฒคํŠธ๋กœ ๋“ฑ๋กํ•˜๊ฒŒ ๋˜๋Š” ๊ณผ์ •์—์„œ ๊ฐ ๋ฉ”์„œ๋“œ์™€ ์ปดํฌ๋„ŒํŠธ์˜ ์ธ์Šคํ„ด์Šค์˜ ๊ด€๊ณ„๊ฐ€ ๋Š๊ฒจ๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ

→ event handlerํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ์˜ this๋Š” ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ์„ ์–ธํ•œ ๋ฉ”์†Œ๋“œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ „์—ญ๊ฐ์ฒด(window)๋ฅผ ์˜๋ฏธ

#TIL19, React | this์™€ bind()

 

#TIL19, React | this์™€ bind()

๊ฐœ์ธ ๊ณต๋ถ€๋ฅผ ์œ„ํ•ด ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹คState & Event์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜๋˜ ์ค‘, this์™€ bind์˜ ๊ฐœ๋…์„ ์ •๋ฆฝํ•˜๊ณ ์ž ๊ธฐ๋กํ•ด๋ณธ๋‹ค.๊ฐ์ฒด์ง€ํ–ฅ ์–ธ์–ด์—์„œ์˜ ์ผ๋ฐ˜์ ์ธ this๋Š” ํ˜„์žฌ ๊ฐ์ฒด๋ฅผ ์ง€์นญํ•˜์ง€๋งŒ, js์˜ this๋Š” ์‹คํ–‰ํ•  ๋•Œ์˜

velog.io

โญ๏ธ ์—˜๋ฆฌ๋จผํŠธ, ์ธ์Šคํ„ด์Šค

์—˜๋ฆฌ๋จผํŠธ(Element): ์‹ค์ œ๋กœ ํ™”๋ฉด์— ๋ Œ๋”๋ง ํ•  DOM ๋…ธ๋“œ๋“ค์˜ ์ •๋ณด๋ฅผ React์—๊ฒŒ ์•Œ๋ ค์ฃผ๊ธฐ ์œ„ํ•œ ์ˆ˜๋‹จ(DOM ์—˜๋ฆฌ๋จผํŠธ, ์ปดํฌ๋„ŒํŠธ ์—˜๋ฆฌ๋จผํŠธ)

  • DOM ์—˜๋ฆฌ๋จผํŠธ: ์—˜๋ฆฌ๋จผํŠธ์˜ type์˜ ํƒœ๊ทธ ์ด๋ฆ„์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ(์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘)
  • React๊ฐ€ ์‹ค์ œ๋กœ ํ™”๋ฉด์— ๋ Œ๋”๋ง ํ•˜๋Š” ๋Œ€์ƒ
  • ์ปดํฌ๋„ŒํŠธ ์—˜๋ฆฌ๋จผํŠธ: ์—˜๋ฆฌ๋จผํŠธ์˜ type์ด ์ปดํฌ๋„ŒํŠธ ํด๋ž˜์Šค/ํ•จ์ˆ˜์ธ ๊ฒฝ์šฐ(๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘)
  • ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์ •์˜ํ•œ ์ปดํฌ๋„ŒํŠธ ํ‘œํ˜„, ์ž…๋ ฅ์œผ๋กœ props๋ฅผ ๋ฐ›์œผ๋ฉด ๋ Œ๋”๋ง ํ•  ์•จ๋ฆฌ๋จผํŠธ ํŠธ๋ฆฌ ๋ฐ˜ํ™˜
  • ํ•œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•˜๋Š”๋ฐ ์žˆ์–ด ๋˜ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

์ปดํฌ๋„ŒํŠธ ์ธ์Šคํ„ด์Šค: ํด๋ž˜์Šค๋กœ ์„ ์–ธ๋œ ์ปดํฌ๋„ŒํŠธ๋“ค๋งŒ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ง

  • ์ปดํฌ๋„ŒํŠธ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ this ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ์ฐธ์กฐํ•˜๋Š” ๋Œ€์ƒ์— ํ•ด๋‹น
  • ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋Š” ์ธ์Šคํ„ด์Šค ๊ฐ–์ง€ ์•Š์Œ

[React] ์—˜๋ฆฌ๋จผํŠธ, ์ปดํฌ๋„ŒํŠธ, ์ธ์Šคํ„ด์Šค ๊ฐœ๋…

 

[React] ์—˜๋ฆฌ๋จผํŠธ, ์ปดํฌ๋„ŒํŠธ, ์ธ์Šคํ„ด์Šค ๊ฐœ๋…

1. ์—˜๋ฆฌ๋จผํŠธ (Element) ์‹ค์ œ๋กœ ํ™”๋ฉด์— ๋ Œ๋”๋ง ํ•  DOM ๋…ธ๋“œ๋“ค์˜ ์ •๋ณด๋ฅผ React์—๊ฒŒ ์•Œ๋ ค์ฃผ๊ธฐ ์œ„ํ•œ ์ˆ˜๋‹จ์ด๋‹ค. DOM ๋…ธ๋“œ ํ˜น์€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” JavaScript์˜ ์ผ๋ฐ˜ ๋ถˆ๋ณ€ ๊ฐ์ฒด(Plain Immutable Object)์— ํ•ด๋‹นํ•œ๋‹ค.

it-eldorado.tistory.com

[ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•]

  1. ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ constructor์—์„œ bind ์ž‘์—…ํ•˜๊ธฐ
    constructor(props) {
        super(props);
        this.handleIncrease = this.handleIncrease.bind(this);
        this.handleDecrease = this.handleDecrease.bind(this);
      }
    
    • super(props)๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์€ ํด๋ž˜์Šค๊ฐ€ ์ปดํฌ๋„ŒํŠธ๋กœ์„œ ์ž‘๋™ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” Component์ชฝ์— ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋จผ์ € ์‹คํ–‰ํ•ด์ฃผ๊ณ , ์ž‘์—… ํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ
  2. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๋ฌธ๋ฒ• ์‚ฌ์šฉ →  ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์‚ฌ์šฉํ•ด์„œ ๋ฉ”์„œ๋“œ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ ํด๋ž˜์Šค์— ํŠน์ • ์†์„ฑ์„ ์„ ์–ธ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” class-properties ๋ฌธ๋ฒ• ์‚ฌ์šฉ(์ •์‹ ๋ฌธ๋ฒ• ์•„๋‹˜)
handleIncrease = () => {
    console.log("increase");
  };
  handleDecrease = () => {
    console.log("decrease");
  };

 

     3. onClick์—์„œ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ(๋น„์ถ”์ฒœ) → ๋ Œ๋”๋ง ํ•  ๋•Œ๋งˆ๋‹ค ํ•จ์ˆ˜๊ฐ€ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์—

return (
      <div>
        <h1>0</h1>
        <button onClick={() => this.handleIncrease()}>+1</button>
        <button onClick={() => this.handleDecrease()}>-1</button>
      </div>
    );
  }

์ƒํƒœ ์„ ์–ธ

  • ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌ ํ•  ๋•Œ์—๋Š” state ์‚ฌ์šฉ
  • state๋Š” ๋ฌด์กฐ๊ฑด ๊ฐ์ฒด ํ˜•ํƒœ
  • state์กฐํšŒํ•˜๋ ค๋ฉด this.state๋ฅผ ์กฐํšŒ
import React, { Component } from "react";

class Counter extends Component {
  constructor(props) {
    super(props);
    this.state = {
      counter: 0,
    };
  }
  handleIncrease = () => {
    console.log("increase");
    console.log(this);
  };
  handleDecrease = () => {
    console.log("decrease");
  };
  render() {
    return (
      <div>
        <h1>{this.state.counter}</h1>
        <button onClick={this.handleIncrease}>+1</button>
        <button onClick={this.handleDecrease}>-1</button>
      </div>
    );
  }
}

export default Counter;

์ƒํƒœ ์—…๋ฐ์ดํŠธ

  • ์ƒํƒœ ์—…๋ฐ์ดํŠธ ํ•  ๋•Œ์—๋Š” this.setState ํ•จ์ˆ˜ ์‚ฌ์šฉ
handleIncrease = () => {
    this.setState({
      counter: this.state.counter + 1,
    });
  };
  handleDecrease = () => {
    this.setState({
      counter: this.state.counter - 1,
    });
  };

→ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์˜ state์—์„œ ๊ฐ์ฒด ํ˜•ํƒœ์˜ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ๋ถˆ๋ณ€์„ฑ์„ ๊ด€๋ฆฌํ•ด์ฃผ๋ฉด์„œ ์—…๋ฐ์ดํŠธ ํ•ด์•ผํ•จ

handleIncrease = () => {
    this.setState({
      counter: this.state.counter + 1,
    });
		this.setState({
		      counter: this.state.counter + 1,
		    });
  };
  • ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด setState๋ฅผ ๋‘ ๋ฒˆ ์‚ฌ์šฉํ•˜๋ฉด์„œ 1์„ ๋”ํ•ด์ฃผ๋Š” ์ž‘์—…์„ ๋‘ ๋ฒˆํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ๋Š” 2๊ฐ€ ๋”ํ•ด์ง€์ง€ ์•Š์Œ
  • setState๋ฅผ ํ•œ๋‹ค๊ณ  ์ƒํƒœ๊ฐ€ ๋ฐ”๋กœ ๋ฐ”๋€Œ๋Š”๊ฒƒ์ด ์•„๋‹˜(๋น„๋™๊ธฐ์ ์œผ๋กœ ์—…๋ฐ์ดํŠธ๋จ). setState๋Š” ๋‹จ์ˆœํžˆ ์ƒํƒœ๋ฅผ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ, ์ƒํƒœ๋กœ ๋ฐ”๊ฟ”๋‹ฌ๋ผ๊ณ  ์š”์ฒญํ•ด์ฃผ๋Š” ํ•จ์ˆ˜

[ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•] ์ฝœ๋ฐฑํ•จ์ˆ˜ ๋„ฃ์–ด์คŒ

handleIncrease = () => {
    this.setState(
      {
        counter: this.state.counter + 1,
      },
      () => {
        console.log(this.state.counter);
      }
    );
  };