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

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

 

https://ko.reactjs.org/docs/getting-started.html

 

 

[Refactoring ํ•˜๋Š” ๋ฒ• - props ์‚ฌ์šฉ]

๋ฆฌํŒฉํ† ๋ง์ด๋ž€?

์™ธ๋ถ€๋™์ž‘์„ ๋ฐ”๊พธ์ง€ ์•Š์œผ๋ฉด์„œ ๋‚ด๋ถ€ ๊ตฌ์กฐ๋ฅผ ๊ฐœ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•

 

Original <App.js>

import React, { Component } from 'react';
import './App.css';

class Subject extends Component {
  render() {
    return (
      <header>
        <h1>WEB</h1>
        world wide web!
      </header>
    );
  }
}
class App extends Component {
  render() {
    return (
      <div className="App">
        <Subject></Subject>
      </div>
    );
  }
}

export default App;

 

props ์ถ”๊ฐ€ํ•œ <App.js>

class Subject extends Component {
  render() {
    return (
      <header>
        <h1>{this.props.title}</h1>
        {this.props.sub}
      </header>
    );
  }
}

class App extends Component {
  render() {
    return (
      <div className="App">
        <Subject title="WEB" sub="world wide web"></Subject>
        <Subject title="React" sub="For UI"></Subject>
      </div>
    );
  }
}

 

Props ์‚ฌ์šฉ ๋ฒ• - ๊ณต์‹๋ฌธ์„œ

ko.reactjs.org/docs/components-and-props.html

 

Components and Props – React

A JavaScript library for building user interfaces

ko.reactjs.org

 

[์ปดํฌ๋„ŒํŠธ ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•˜๊ธฐ]

Original <App.js>

import React, { Component } from 'react';
import './App.css';

class Subject extends Component {
  render() {
    return (
      <header>
        <h1>WEB</h1>
        world wide web!
      </header>
    );
  }
}

class TOC extends Component {
  render() {
    return (
      <nav>
        <ul>
            <li><a href="1.html">HTML</a></li>
            <li><a href="2.html">CSS</a></li>
            <li><a href="3.html">JavaScript</a></li>
        </ul>
      </nav>
    );
  }
}

class Content extends Component {
  render() {
    return (
      <article>
        <h2>HTML</h2>
        HTML is HyperText Markup Language.
      </article>
    );
  }
}

class App extends Component {
  render() {
    return (
      <div className="App">
        <Subject></Subject>
        <TOC></TOC>
        <Content></Content>
      </div>
    );
  }
}

export default App;

 

<TOC.js>

import React, { Component } from 'react';

class TOC extends Component {
    render() {
      return (
        <nav>
          <ul>
              <li><a href="1.html">HTML</a></li>
              <li><a href="2.html">CSS</a></li>
              <li><a href="3.html">JavaScript</a></li>
          </ul>
        </nav>
      );
    }
  }

export default TOC;

 

<Content.js>

import React, { Component } from 'react';

class Content extends Component {
    render() {
      return (
        <article>
          <h2>{this.props.title}</h2>
          {this.props.desc}
        </article>
      );
    }
  }

export default Content;

 

<Subject.js>

import React, { Component } from 'react';

class Subject extends Component {
    render() {
      return (
        <header>
          <h1>{this.props.title}</h1>
          {this.props.sub}
        </header>
      );
    }
  }

export default Subject;

 

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 {
  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;

 

์›๋ž˜ App.js ํŒŒ์ผ์—์„œ 3๊ฐœ์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ๊ฐ TOC.js, Content.js, Subject.js ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ–ˆ๋‹ค.

App.js ํŒŒ์ผ์—๋Š” ๊ผญ import๋ฅผ ํ•ด์ค€๋‹ค!

๋Œ“๊ธ€