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

์ด ๊ธ€์€ ์ƒํ™œ์ฝ”๋”ฉ React ๊ฐ•์˜[opentutorials.org/module/4058]๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

 

 

[State๋ž€?]

  • Component : ์‚ฌ์šฉ์ž๊ฐ€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ž…์žฅ์—์„œ ์ค‘์š”
  • Props : ์ปดํฌ๋„ŒํŠธ์˜ ์†์„ฑ, ์‚ฌ์šฉ์ž์—๊ฒŒ ์ค‘์š”, ์™ธ๋ถ€
  • State : props์˜ ๊ฐ’์— ๋”ฐ๋ผ ๋‚ด๋ถ€ ๊ตฌํ˜„์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ

=> props์™€ state๋ฅผ ์ฒ ์ €ํžˆ ๋ถ„๋ฆฌ!

 

 

 

[State ์‚ฌ์šฉ]

Original <App.js>

import React, { Component } from 'react';
import './App.css';
import TOC from "./components/TOC";
import Content from "./components/Content";
import Subject from "./components/Subject";

class App extends Component {
  render() {
    return (
      <div className="App">
        <Subject title="WEB" sub="world wide web"></Subject>
        <Subject title="React" sub="For UI"></Subject>
        <TOC></TOC>
        <Content title="HTML" desc="HTML is HyperText Markup Language."></Content>
      </div>
    );
  }
}

export default App;

 

 

Changed <App.js>

import React, { Component } from 'react';
import './App.css';
import TOC from "./components/TOC";
import Content from "./components/Content";
import Subject from "./components/Subject";


class App extends Component {
  constructor(props){
    super(props);
    this.state = {
      subject:{title:"WEB", sub:"world wide web"}
    } //state๊ฐ’ ์ดˆ๊ธฐํ™”
  } //component ์•ˆ์˜ constructor๋Š” ๊ฐ€์žฅ ๋จผ์ € ์‹คํ–‰๋˜๋ฉฐ ์ดˆ๊ธฐํ™”๋ฅผ ๋‹ด๋‹น

  render() {
    return (
      <div className="App">
        <Subject 
          title={this.state.subject.title}
          sub ={this.state.subject.sub}>
        </Subject>
        <Subject title="React" sub="For UI"></Subject>
        <TOC></TOC>
        <Content title="HTML" desc="HTML is HyperText Markup Language."></Content>
      </div>
    );
  }
}

export default App;

 

 

[Key ์‚ฌ์šฉ]

๊ฐ๊ฐ์˜ ํ•ญ๋ชฉ๋“ค์€ key๋ผ๊ณ  ํ•˜๋Š” props๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผํ•จ

 

<TOC.js>

import React, { Component } from 'react';

class TOC extends Component {
    render() {
      var lists = [];
      var data = this.props.data;
      var i = 0;
      while(i < data.length){
        lists.push(<li key={data[i].id}><a href={"/contect/"+data[i].id}>{data[i].title}</a></li>);
        i = i + 1;
      }
      return (
        <nav>
          <ul>
            {lists}
          </ul>
        </nav>
      );
    }
  }

export default TOC;

 

<App.js>

import React, { Component } from 'react';
import './App.css';
import TOC from "./components/TOC";
import Content from "./components/Content";
import Subject from "./components/Subject";


class App extends Component {
  constructor(props){
    super(props);
    this.state = {
      subject:{title:"WEB", sub:"world wide web"},
      contents:[
        {id:1, title:'HTML', desc:'HTML is for information'},
        {id:2, title:'CSS', desc:'CSS is for design'},
        {id:3, title:'JavaScript', desc:'JavaScript is for interactive'}
      ]
    } //state๊ฐ’ ์ดˆ๊ธฐํ™”
  } //component ์•ˆ์˜ constructor๋Š” ๊ฐ€์žฅ ๋จผ์ € ์‹คํ–‰๋˜๋ฉฐ ์ดˆ๊ธฐํ™”๋ฅผ ๋‹ด๋‹น

  render() {
    return (
      <div className="App">
        <Subject 
          title={this.state.subject.title}
          sub ={this.state.subject.sub}>
        </Subject>
        <Subject title="React" sub="For UI"></Subject>
        <TOC data={this.state.contents}></TOC>
        <Content title="HTML" desc="HTML is HyperText Markup Language."></Content>
      </div>
    );
  }
}

export default App;

 

๋Œ“๊ธ€