React JS DeepDive
โš›๏ธ

React JS DeepDive

Tags
React
Property
Published
May 9, 2021
slug
reactjs

React JS

๋ฆฌ์•กํŠธ๋Š” ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž…๋‹ˆ๋‹ค.

๋ฆฌ์•กํŠธ ํŠน์ง•

1. ์„ ์–ธํ˜•

React๋Š” ์ƒํ˜ธ์ž‘์šฉ์ด ๋งŽ์€ UI๋ฅผ ๋งŒ๋“ค ๋•Œ ์ƒ๊ธฐ๋Š” ์–ด๋ ค์›€์„ ์ค„์—ฌ์ค๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฐ ์ƒํƒœ์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ๋ทฐ๋งŒ ์„ค๊ณ„ํ•˜์„ธ์š”. ๊ทธ๋Ÿผ React๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋จ์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ์ปดํฌ๋„ŒํŠธ๋งŒ ํšจ์œจ์ ์œผ๋กœ ๊ฐฑ์‹ ํ•˜๊ณ  ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.
์„ ์–ธํ˜• ๋ทฐ๋Š” ์ฝ”๋“œ๋ฅผ ์˜ˆ์ธก ๊ฐ€๋Šฅํ•˜๊ณ  ๋””๋ฒ„๊ทธํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.
ย 

2. ์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜

์Šค์Šค๋กœ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์บก์Šํ™”๋œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“œ์„ธ์š”. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ์กฐํ•ฉํ•ด ๋ณต์žกํ•œ UI๋ฅผ ๋งŒ๋“ค์–ด๋ณด์„ธ์š”.
์ปดํฌ๋„ŒํŠธ ๋กœ์ง์€ ํ…œํ”Œ๋ฆฟ์ด ์•„๋‹Œ JavaScript๋กœ ์ž‘์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์–‘ํ•œ ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์•ฑ ์•ˆ์—์„œ ์†์‰ฝ๊ฒŒ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๊ณ , DOM๊ณผ๋Š” ๋ณ„๊ฐœ๋กœ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ย 

3. ์–ด๋””์„œ๋‚˜ ์‚ฌ์šฉ๊ฐ€๋Šฅ

๊ธฐ์ˆ  ์Šคํƒ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์—๋Š” ๊ด€์—ฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ ๋„ React์˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ด์šฉํ•ด ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
React๋Š” Node ์„œ๋ฒ„์—์„œ ๋ Œ๋”๋ง์„ ํ•  ์ˆ˜๋„ ์žˆ๊ณ ,ย React Native๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ชจ๋ฐ”์ผ ์•ฑ๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ย 
ย 

์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ

1. ๊ฐ„๋‹จํ•œ ์ปดํฌ๋„ŒํŠธ

React ์ปดํฌ๋„ŒํŠธ๋Š”ย render()๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ, ์ด๊ฒƒ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ํ™”๋ฉด์— ํ‘œ์‹œํ•  ๋‚ด์šฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” XML๊ณผ ์œ ์‚ฌํ•œ ๋ฌธ๋ฒ•์ธ JSX๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ๋Š”ย render()ย ์•ˆ์—์„œย this.props๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
React๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ JSX๊ฐ€ ๊ผญ ํ•„์š”ํ•œ ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค.ย JSX๋ฅผ ์ปดํŒŒ์ผํ•œ JavaScript ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉดย Babel REPL์„ ์ด์šฉํ•ด๋ณด์„ธ์š”.
ย 
class HelloMessage extends React.Component {
  render() {
    return (
      <div>
        Hello {this.props.name}
      </div>
    );
  }
}

ReactDOM.render(
  <HelloMessage name="Taylor" />,
  document.getElementById('hello-example')
);
ย 

2. ์ƒํƒœ๋ฅผ ๊ฐ€์ง€๋Š” ์ปดํฌ๋„ŒํŠธ

์ปดํฌ๋„ŒํŠธ๋Š”ย this.props๋ฅผ ์ด์šฉํ•ด ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ฒƒ ์™ธ์—๋„ ๋‚ด๋ถ€์ ์ธ ์ƒํƒœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š”ย this.state๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ”๋€Œ๋ฉดย render()๊ฐ€ ๋‹ค์‹œ ํ˜ธ์ถœ๋˜์–ด ๋งˆํฌ์—…์ด ๊ฐฑ์‹ ๋ฉ๋‹ˆ๋‹ค.
class Timer extends React.Component {
  constructor(props) {
    super(props);
    this.state = { seconds: 0 };
  }

  tick() {
    this.setState(state => ({
      seconds: state.seconds + 1
    }));
  }

  componentDidMount() {
    this.interval = setInterval(() => this.tick(), 1000);
  }
ย 

3. React Todo App

class TodoApp extends React.Component {
  constructor(props) {
    super(props);
    this.state = { items: [], text: '' };
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  render() {
    return (
      <div>
        <h3>TODO</h3>
        <TodoList items={this.state.items} />
        <form onSubmit={this.handleSubmit}>
          <label htmlFor="new-todo">
            What needs to be done?
          </label>
          <input
            id="new-todo"
            onChange={this.handleChange}
            value={this.state.text}
          />
          <button>
            Add #{this.state.items.length + 1}
          </button>
        </form>
      </div>
    );
  }

  handleChange(e) {
    this.setState({ text: e.target.value });
  }

  handleSubmit(e) {
    e.preventDefault();
    if (this.state.text.length === 0) {
      return;
    }
    const newItem = {
      text: this.state.text,
      id: Date.now()
    };
    this.setState(state => ({
      items: state.items.concat(newItem),
      text: ''
    }));
  }
}

class TodoList extends React.Component {
  render() {
    return (
      <ul>
        {this.props.items.map(item => (
          <li key={item.id}>{item.text}</li>
        ))}
      </ul>
    );
  }
}

ReactDOM.render(
  <TodoApp />,
  document.getElementById('todos-example')
);
ย 

4. ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์‚ฌ์šฉํ•œ ์ปดํฌ๋„ŒํŠธ

class MarkdownEditor extends React.Component {
  constructor(props) {
    super(props);
    this.md = new Remarkable();
    this.handleChange = this.handleChange.bind(this);
    this.state = { value: 'Hello, **world**!' };
  }

  handleChange(e) {
    this.setState({ value: e.target.value });
  }

  getRawMarkup() {
    return { __html: this.md.render(this.state.value) };
  }

  render() {
    return (
      <div className="MarkdownEditor">
        <h3>Input</h3>
        <label htmlFor="markdown-content">
          Enter some markdown
        </label>
        <textarea
          id="markdown-content"
          onChange={this.handleChange}
          defaultValue={this.state.value}
        />
        <h3>Output</h3>
        <div
          className="content"
          dangerouslySetInnerHTML={this.getRawMarkup()}
        />
      </div>
    );
  }
}

ReactDOM.render(
  <MarkdownEditor />,
  document.getElementById('markdown-example')
);