ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

https://ko.reactjs.org/docs/rendering-elements.html

 

 

 

์ด์ „ ๋‚ด์šฉ ๋ณต์Šต

[WEB] - [React] ๊ณต์‹ ๋ฌธ์„œ 3. ์—˜๋ฆฌ๋จผํŠธ ๋ Œ๋”๋ง

 

[React] ๊ณต์‹ ๋ฌธ์„œ 3. ์—˜๋ฆฌ๋จผํŠธ ๋ Œ๋”๋ง

3. ์—˜๋ฆฌ๋จผํŠธ ๋ Œ๋”๋ง ์—˜๋ฆฌ๋จผํŠธ React ์•ฑ์˜ ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„ ์ผ๋ฐ˜ ๊ฐ์ฒด React DOM์€ React ์—˜๋ฆฌ๋จผํŠธ์™€ ์ผ์น˜ํ•˜๋„๋ก DOM์„ ์—…๋ฐ์ดํŠธ ๋ถˆ๋ณ€ ๊ฐ์ฒด, ์—˜๋ฆฌ๋จผํŠธ ์ƒ์„ฑ ์ดํ›„ ํ•ด๋‹น ์—˜๋ฆฌ๋จผํŠธ์˜ ์ž์‹์ด๋‚˜ ์†์„ฑ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€

nakyungim.tistory.com

  • UI๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•
    • ์ƒˆ๋กœ์šด ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ƒ์„ฑ ํ›„ ์ด๋ฅผ ReactDOM.render()๋กœ ์ „๋‹ฌ
  • ๋ Œ๋”๋ง ๋œ ์ถœ๋ ฅ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด ReactDOM.render() ํ˜ธ์ถœ

 

 

 

 

5. State์™€ ์ƒ๋ช…์ฃผ๊ธฐ

 

๋ชฉํ‘œ :

1. Clock ์ปดํฌ๋„ŒํŠธ๋ฅผ ์™„์ „ํžˆ ์žฌ์‚ฌ์šฉํ•˜๊ณ  ์บก์Šํ™” ํ•˜๊ธฐ

2. ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์Šค์Šค๋กœ ํƒ€์ด๋จธ ์„ค์ • and ๋งค์ดˆ ์Šค์Šค๋กœ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ

 

 

์›๋ž˜ index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';

function tick(){
  const element = (
    <div>
      <h1>Hello, world!</h1>
      <h2>It is {new Date().toLocaleTimeString()}.</h2>
    </div>
  );
  ReactDOM.render(
    element,
    document.getElementById('root')
  );
}

setInterval(tick, 1000);

 

 

1. Clock ์ปดํฌ๋„ŒํŠธ๋ฅผ ์™„์ „ํžˆ ์žฌ์‚ฌ์šฉํ•˜๊ณ  ์บก์Šํ™” ํ•˜๊ธฐ

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';

function Clock(props){
  return(
    <div>
      <h1>Hello, world!</h1>
      <h2>It is {props.date.toLocaleTimeString()}.</h2>
    </div>
  );
}

function tick(){
  ReactDOM.render(
    <Clock date={new Date()} />,
    document.getElementById('root')
  );
}

setInterval(tick, 1000);

 

 

2. ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์Šค์Šค๋กœ ํƒ€์ด๋จธ ์„ค์ • and ๋งค์ดˆ ์Šค์Šค๋กœ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ

-> Clock ์ปดํฌ๋„ŒํŠธ์— state ์ถ”๊ฐ€

 

 

 

 

ํ•จ์ˆ˜์—์„œ ํด๋ž˜์Šค๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ -> ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ๋ณ€ํ™˜ 5๋‹จ๊ณ„

1. React.Component๋ฅผ ํ™•์žฅํ•˜๋Š” ๋™์ผํ•œ ์ด๋ฆ„์˜ ES6 class๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

-> Clock.js ์ƒ์„ฑ

 

2. render()๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๋นˆ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

-> function Clock(props)์„ class Clock extends Component๋กœ ๋ณ€๊ฒฝ

-> ํด๋ž˜์Šค ๋‚ด๋ถ€์— render() ๋ฉ”์†Œ๋“œ ์ถ”๊ฐ€

 

3. ํ•จ์ˆ˜์˜ ๋‚ด์šฉ์„ render() ๋ฉ”์„œ๋“œ ์•ˆ์œผ๋กœ ์˜ฎ๊น๋‹ˆ๋‹ค.

-> ํ•จ์ˆ˜ ๋‚ด๋ถ€ ๋‚ด์šฉ์„ render() ๋‚ด๋ถ€๋กœ ์ด๋™

 

4. render() ๋‚ด์šฉ ์•ˆ์— ์žˆ๋Š” props๋ฅผ this.props๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

-> props ์•ž์— this. ์ถ”๊ฐ€

 

5. ๋‚จ์•„์žˆ๋Š” ๋นˆ ํ•จ์ˆ˜ ์„ ์–ธ์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

 

<Clock.js>

import React from 'react';

class Clock extends React.Component{
    render(){
        return(
            <div>
                <h1>Hello, world!</h1>
                <h2>It is {this.props.date.toLocaleTimeString()}.</h2>
            </div>
        );
    }
}

export default Clock;
  • render() ๋ฉ”์†Œ๋“œ๋Š” ์—…๋ฐ์ดํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ
  • ๊ฐ™์€ DOM ๋…ธ๋“œ๋กœ <Clock />์„ ๋ Œ๋”๋งํ•˜๋Š” ๊ฒฝ์šฐ, Clock ํด๋ž˜์Šค์˜ ๋‹จ์ผ ์ธ์Šคํ„ด์Šค๋งŒ ์‚ฌ์šฉ
    -> ๋กœ์ปฌ state์™€ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์†Œ๋“œ์™€ ๊ฐ™์€ ๋ถ€๊ฐ€์ ์ธ ๊ธฐ๋Šฅ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•จ

 

 

 

 

ํด๋ž˜์Šค์— ๋กœ์ปฌ State ์ถ”๊ฐ€ํ•˜๊ธฐ

date๋ฅผ props์—์„œ state๋กœ ์ด๋™ํ•˜๊ธฐ 3๋‹จ๊ณ„

1. render() ๋ฉ”์„œ๋“œ ์•ˆ์— ์žˆ๋Š” this.props.date๋ฅผ this.state.date๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

-> props๋ฅผ state๋กœ ๋ณ€๊ฒฝ

 

2. ์ดˆ๊ธฐ this.state๋ฅผ ์ง€์ •ํ•˜๋Š” class constructor๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

    constructor(props){
        super(props);
        this.state = {date: new Date()};
    } // ์ดˆ๊ธฐ state ์ง€์ •
  • ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๋Š” ํ•ญ์ƒ props๋กœ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž ํ˜ธ์ถœ

 

3. <Clock /> ์š”์†Œ์—์„œ date prop์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

 

 

<Clock.js>

import React from 'react';

class Clock extends React.Component{
    constructor(props){
        super(props);
        this.state = {date: new Date()};
    } // ์ดˆ๊ธฐ state ์ง€์ •

    render(){
        return(
            <div>
                <h1>Hello, world!</h1>
                <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
            </div>
        );
    }
}

export default Clock;

 

<index.js>

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import Clock from './Clock';

function tick(){
  ReactDOM.render(
    <Clock />,
    document.getElementById('root')
  );
}

setInterval(tick, 1000);

 

=> ์—ฌ๊ธฐ๊นŒ์ง€ ์™„๋ฃŒํ•˜๊ณ  ๋ Œ๋”๋งํ•˜๋ฉด ์ดˆ ์—…๋ฐ์ดํŠธ๊ฐ€ ์•ˆ๋˜๋Š” ๊ฒƒ์ด ๋งž์Œ! ์ด์ œ๋ถ€ํ„ฐ ํ• ๊ฑฐ์—์š”~

 

 

 

 

์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ๋ฅผ ํด๋ž˜์Šค์— ์ถ”๊ฐ€ํ•˜๊ธฐ

๋งˆ์šดํŒ… : Clock์ด ์ฒ˜์Œ DOM์— ๋ Œ๋”๋ง ๋  ๋•Œ๋งˆ๋‹ค ํƒ€์ด๋จธ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

์–ธ๋งˆ์šดํŒ… : Clock์— ์˜ํ•ด ์ƒ์„ฑ๋œ DOM์ด ์‚ญ์ œ๋  ๋•Œ๋งˆ๋‹ค ํƒ€์ด๋จธ๋ฅผ ํ•ด์ œํ•œ๋‹ค.

 

๋งŽ์€ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‚ญ์ œ๋  ๋•Œ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‚ฌ์šฉ ์ค‘์ด๋˜ ๋ฆฌ์†Œ์Šค๋ฅผ ํ™•๋ณดํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”!

 

์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ ์„ ์–ธํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ or ์–ธ๋งˆ์šดํŠธ ๋  ๋•Œ ์ผ๋ถ€ ์ฝ”๋“œ๋ฅผ ์ž‘๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉ๋œ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ๋Š”

constructor

componentDidMount

componentWillUnmount

render

 

<Clock.js>

import React from 'react';

class Clock extends React.Component{
    constructor(props){
        super(props);
        this.state = {date: new Date()};
    } // ์ดˆ๊ธฐ state ์ง€์ •

    componentDidMount(){
        this.timerID = setInterval(
            () => this.tick(),
            1000
        ) // ํƒ€์ด๋จธ ์„ค์ •(timerID ์ €์žฅ)
    } // ์ปดํฌ๋„ŒํŠธ ์ถœ๋ ฅ๋ฌผ์ด DOM์— ๋ Œ๋”๋ง ๋œ ํ›„์— ์‹คํ–‰

    componentWillUnmount(){
        clearInterval(this.timerID);
    } // ํƒ€์ด๋จธ ๋ถ„ํ•ด

    tick() {
        this.setState({
            date: new Date()
        }); // setState๋Š” ์ปดํฌ๋„ŒํŠธ ๋กœ์ปฌ state ์—…๋ฐ์ดํŠธ
    } // Clock ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งค์ดˆ ์ž‘๋™ํ•˜๋„๋ก ํ•˜๋Š” ๋ฉ”์„œ๋“œ

    render(){
        return(
            <div>
                <h1>Hello, world!</h1>
                <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
            </div>
        );
    }
}

export default Clock;

 

 

 

 

State๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ

setState() ์— ๋Œ€ํ•ด์„œ ์•Œ์•„์•ผ ํ•  3๊ฐ€์ง€

 

1. ์ง์ ‘ State๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ๋ง๊ธฐ -> ๋Œ€์‹  setState() ์‚ฌ์šฉ!

this.state.comment = 'Hi'; // Wrong
this.setState({comment: 'Hi'}); // Correct

 

* this.state๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์ผํ•œ ๊ณต๊ฐ„์€ constructor

 

 

2. State ์—…๋ฐ์ดํŠธ๋Š” ๋น„๋™๊ธฐ์ ์ผ ์ˆ˜๋„ ์žˆ๋‹ค.

// Wrong
this.setState({
  counter: this.state.counter + this.props.increment,
});

// Correct (arrow function ์‚ฌ์šฉ)
this.setState((state, props) => ({
  counter: state.counter + props.increment
}));

// Correct (arrow function ์‚ฌ์šฉx)
this.setState(function(state, props) {
  return {
    counter: state.counter + props.increment
  };
});

 

 

3. State ์—…๋ฐ์ดํŠธ๋Š” ๋ณ‘ํ•ฉ๋œ๋‹ค.

state๋Š” ๋‹ค์–‘ํ•œ ๋…๋ฆฝ๋ณ€์ˆ˜๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Œ

  constructor(props) {
    super(props);
    this.state = {
      posts: [],
      comments: []
    };
  }

 

๋ณ„๋„์˜ setState() ํ˜ธ์ถœ๋กœ ๋ณ€์ˆ˜๋ฅผ ๋…๋ฆฝ์ ์œผ๋กœ ์—…๋ฐ์ดํŠธ ๊ฐ€๋Šฅ

  componentDidMount() {
    fetchPosts().then(response => {
      this.setState({
        posts: response.posts
      });
    });

    fetchComments().then(response => {
      this.setState({
        comments: response.comments
      });
    });
  }

 

๋ณ‘ํ•ฉ์€ ์–•๊ฒŒ ์ด๋ฃจ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— this.setState({comments})๋Š” this.state.posts์— ์˜ํ–ฅ์„ ์ฃผ์ง„ ์•Š์ง€๋งŒ this.state.comments๋Š” ์™„์ „ํžˆ ๋Œ€์ฒด๋œ๋‹ค.

 

 

 

The Data Flows Down (๋ฐ์ดํ„ฐ๋ฅผ ์•„๋ž˜๋กœ ํ๋ฅธ๋‹ค.)

์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹ ์˜ state๋ฅผ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์— props๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

"ํ•˜ํ–ฅ์‹(top-down)" ๋˜๋Š” "๋‹จ๋ฐฉํ–ฅ์‹" ๋ฐ์ดํ„ฐ ํ๋ฆ„

 

 

 

 

์ „์ฒด ์ฝ”๋“œ ๋งํฌ

github.com/NakyungIm/React/tree/master/clock-app

 

NakyungIm/React

Contribute to NakyungIm/React development by creating an account on GitHub.

github.com

 

๋Œ“๊ธ€