props, state - MoonGyeongHyeon/React_Study GitHub Wiki

React - props, state

Nested Elements

์ปดํฌ๋„ŒํŠธ์—์„œ ์—ฌ๋Ÿฌ Element๋ฅผ ๋ Œ๋”๋งํ•ด์•ผ ํ•  ๋•Œ, ๊ทธ element๋“ค์„ ํ•„์ˆ˜์ ์œผ๋กœ container element ์•ˆ์— ํฌํ•จ์‹œ์ผœ์•ผ ํ•œ๋‹ค.

 return  (
            <h1> Hello Velopert</h1>
            <h2> Welcome </h2>
        );

์œ„ ์ฝ”๋“œ๋Š”

Module build failed: SyntaxError: /home/vlpt/node_tutorial/react/react-tutorials/03-jsx/src/App.js: Adjacent JSX elements must be wrapped in an enclosing tag (10:12)

์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค.

  return  (
            <div>
                <h1> Hello Velopert </h1>
                <h2> Welcome </h2>
            </div>
        );

props

์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ ์ค‘ ๋ณ€๋™๋˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. parent ์ปดํฌ๋„ŒํŠธ์—์„œ child ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „ํ•  ๋•Œ 'props'๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค.

  • props ์ถ”๊ฐ€ํ•˜๊ธฐ

    ์ปดํฌ๋„ŒํŠธ์—์„œ immutable(๋ถˆ๋ณ€) ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•  ๋•, render() ํ•จ์ˆ˜์˜ ๋‚ด๋ถ€์— {this.props.propsName} ํ˜•์‹์œผ๋กœ ๋„ฃ๊ณ , ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, < > ๊ด„ํ˜ธ ์•ˆ์— propsName="Value" ๋ฅผ ๋„ฃ์–ด ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค.

  • ์˜ˆ์ œ

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';

const rootElement = document.getElementById('root');
ReactDOM.render(<App headerTitle="Welcome!" 
                     contentTitle="Stranger"
                     contentBody="Welcome to example app"/>, rootElement);

App.js

import React from 'react';
import Header from './Header';
import Content from './Content';

class App extends React.Component {
    render(){
        return  (
            <div>
                <Header title={this.props.headerTitle}/>
                <Content title={this.props.contentTitle} content={this.props.contentBody}/>
            </div>
        );
    }
}

export default App;

Header.js

import React from 'react';

class Header extends React.Component {
    render(){
        return (
            <h1>{this.props.title}</h1>
        );
    }
}

export default Header;

Content.js

import React from 'react';

class Content extends React.Component {
    render(){
        return (
            <div>
                <h2>{this.props.title}</h2>
                <p>{this.props.content}</p>
            </div>
        );
    }
}

export default Content;

๊ธฐ๋ณธ๊ฐ’ ์„ค์ •

props ๊ฐ’์„ ์ž„์˜๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ์‚ฌ์šฉํ•  ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ๋•, ์ปดํฌ๋„ŒํŠธ ํด๋ž˜์Šค ํ•˜๋‹จ์— className.defaultProps={propName: value} ๋ฅผ ์‚ฝ์ž…ํ•˜๋ฉด ๋œ๋‹ค.

  • ์˜ˆ์ œ

    ์œ„ ์˜ˆ์ œ์—์„œ App.js ์— ์•ฝ๊ฐ„์˜ ์ฝ”๋“œ๋งŒ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

App.js

import React from 'react';
import Header from './Header';
import Content from './Content';

class App extends React.Component {
    render(){
        return  (
            <div>
                <Header title={this.props.headerTitle}/>
                <Content title={this.props.contentTitle} content={this.props.contentBody}/>
            </div>
        );
    }
}

/*
 * ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€
 */
App.defaultProps = {
    headerTitle: 'Default header',
    contentTitle: 'Default contentTitle',
    contentBody: 'Default contentBody'
};

export default App;

State

์ปดํฌ๋„ŒํŠธ์—์„œ ์œ ๋™์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ state ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. React.js ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค ๋• state ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ตœ์†Œํ™”ํ•ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 10๊ฐœ์˜ ์ปดํฌ๋„ŒํŠธ์—์„œ ์œ ๋™์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ๋•, ๊ฐ ๋ฐ์ดํ„ฐ์— state๋ฅผ ์‚ฌ์šฉํ•  ๊ฒŒ ์•„๋‹ˆ๋ผ, props ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  10๊ฐœ์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํฌํ•จ์‹œํ‚ค๋Š” container ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ด๋‹ค.

  • ์˜ˆ์ œ

App.js

import React from 'react';
import StateExample from "./StateExample";

class App extends React.Component {
    render() {
        return (
            <div>
                <StateExample/>
            </div>
        );
    }
}

export default App;

StateExample.js

import React from 'react';

class StateExample extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            header: "Header Initial state",
            content: "Content Initial State"
        };
    }

    updateHeader(text){
        this.setState({
            header: "Header has changed"
        });
    }

    render() {
        return (
            <div>
                <h1>{this.state.header}</h1>
                <h2>{this.state.content}</h2>
                <button onClick={this.updateHeader.bind(this)}>Update</button>
            </div>
        );
    }
}

export default StateExample;
  1. state์˜ ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•  ๋•Œ๋Š” constructor(์ƒ์„ฑ์ž) ํ•จ์ˆ˜์—์„œ this.state = { } ๋ฅผ ํ†ตํ•˜์—ฌ ์„ค์ •ํ•œ๋‹ค.
  2. state๋ฅผ ๋ Œ๋”๋งํ•  ๋•Œ๋Š” {this. state. stateName} ์„ ์‚ฌ์šฉํ•œ๋‹ค.
  3. state๋ฅผ ์—…๋ฐ์ดํŠธํ•  ๋•Œ๋Š” this.setState() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ES6(ES2015)์—์„œ๋Š” 'auto binding'์ด ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ, setState ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ํ•จ์ˆ˜๋ฅผ bind ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. (bindํ•˜์ง€ ์•Š์œผ๋ฉด React Component๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ๋ฐ ๊ฐ์ฒด์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค.)

props vs state

ํŠน์„ฑ props state
parent ์ปดํฌ๋„ŒํŠธ์— ์˜ํ•ด ๊ฐ’์ด ๋ณ€๊ฒฝ ๋  ์ˆ˜ ์žˆ๋Š”๊ฐ€? ์˜ˆ ์•„๋‹ˆ์˜ค
์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๋ณ€๊ฒฝ ๋  ์ˆ˜ ์žˆ๋Š”๊ฐ€? ์•„๋‹ˆ์˜ค ์˜ˆ
โš ๏ธ **GitHub.com Fallback** โš ๏ธ