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

 

React - 16.1 ์ด๋ฒคํŠธ state props ๊ทธ๋ฆฌ๊ณ  render ํ•จ์ˆ˜

- ๋ชฉํ‘œ : WEB์— ๋งํฌ๋ฅผ ๊ฑธ๊ณ  ๋งํฌ๋ฅผ ํด๋ฆญํ•˜๋ฉด welcome์— ํ•ด๋‹น๋˜๋Š” ๋ฉ”์‹œ์ง€๊ฐ€ ๋‚˜์˜ค๊ฒŒ ํ•  ๊ฒƒ

- ํด๋ฆญํ•œ ๊ฒƒ์— ํ•ด๋‹น๋˜๋Š” content ์ถœ๋ ฅ๋˜๊ฒŒ ํ•จ

 

- render() ํ•จ์ˆ˜ : ์–ด๋–ค ํ•จ์ˆ˜๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ๊ฐ™์ด ๋ฐ”๋€œ

import { Component } from 'react';
import TOC from './component/TOC';
import Content from './component/Content';
import Subject from './component/Subject';
import './App.css';

class App extends Component {
  constructor(props){
    super(props)
    this.state = {
      mode: 'welcome',
      subject:{title: 'WEB', sub: 'World Wide Web!'},
      welcome:{title: 'Welcome', desc: 'Hello React!!'},
      contents:[
        {id:1, title:'HTML', desc:'HTML is for information'},
        {id:2, title:'CSS', desc:'HTML is for design'},
        {id:3, title:'JavaScript', desc:'HTML is for interactive'}
      ]
    }
  }

  render() {
    console.log('App render');
    var _title, _desc = null;
    if(this.state.mode === 'welcome'){
      _title = this.state.welcome.title;
      _desc = this.state.welcome.desc;
    }
    else if(this.state.mode === 'read'){
        _title = this.state.contents[0].title;
        _desc = this.state.contents[0].desc;
    }
    return (
      <div className="App">
        <Subject 
        title = {this.state.subject.title} 
        sub = {this.state.subject.sub}>
        </Subject>
        <TOC data = {this.state.contents}></TOC>
        <Content title = "HTML" desc = {_desc}></Content>
      </div>
    );
  }
}

export default App;

- mode๋ฅผ welcome์œผ๋กœ ํ•ด ๋†“๊ณ  render ํ•จ์ˆ˜์— mode๊ฐ€ welcome์ผ ๊ฒฝ์šฐ welcome.state์™€ welcome.desc๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋งŒ๋“ค์—ˆ์Œ

- read์ผ ๊ฒฝ์šฐ์—๋Š” content์˜ ์ฒซ๋ฒˆ์งธ ํ•ญ๋ชฉ์„ ํ˜ธ์ถœํ•˜๊ฒŒ ๋งŒ๋“ฌ

 

<header>
            <h1><a href ="/">{this.props.title}</a></h1>
            {this.props.sub}
        </header>

- subject.js์— ๋งํฌ ํƒœ๊ทธ ์ถ”๊ฐ€

 

- ๊ฐœ๋ฐœ์ž ๋„๊ตฌ๋กœ ๋ณด์•˜์„ ๋•Œ mode๊ฐ€ welcome์ด๋ฉด Hello React๊ฐ€ ๋œธ, read์ผ ๋• ๋‹ค๋ฅธ ์ž๋ง‰์ด ๋œธ

 

React - 16.2 ์ด๋ฒคํŠธ ์‹ค์‹œ
<div className="App">
        {/*<Subject 
        title = {this.state.subject.title} 
        sub = {this.state.subject.sub}>
        </Subject>*/}
        <header>
            <h1><a href ="/" onClick={function(){
              alert('hi');
            }}>{this.state.subject.title}</a></h1>
            {this.state.subject.sub}
        </header>

- App.java ์ˆ˜์ •

- Subject ํƒœ๊ทธ๋ฅผ ์ฃผ์„์ฒ˜๋ฆฌํ•˜๊ณ  Subject.java์—์„œ ํ—ค๋” ํƒœ๊ทธ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์™€์„œ onClick ์†์„ฑ ์ถ”๊ฐ€

 

- ์ด๋ ‡๊ฒŒ WEB์„ ํด๋ฆญํ•˜๋ฉด ์ฐฝ์ด ๋œจ์ง€๋งŒ ํ™•์ธ์„ ๋ˆ„๋ฅด๋ฉด ํŽ˜์ด์ง€๊ฐ€ ๋ฆฌ๋กœ๋“œ

 

<header>
            <h1><a href ="/" onClick={function(e){
              console.log(e);
              e.preventDefault();
              alert('hi');
            }}>{this.state.subject.title}</a></h1>
            {this.state.subject.sub}
        </header>

- preventDefault()๋ฅผ ์ถ”๊ฐ€ํ•ด ํŽ˜์ด์ง€๊ฐ€ ๋ฆฌ๋กœ๋“œ๋˜์ง€ ์•Š๊ฒŒ ํ•จ

 

React - 16.3 ์ด๋ฒคํŠธ์—์„œ state ๋ณ€๊ฒฝํ•˜๊ธฐ

- WEB์„ ํด๋ฆญํ–ˆ์„ ๋•Œ App์ด๋ผ๊ณ  ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ๋ชจ๋“œ๊ฐ’์„ welcome์œผ๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ์Œ

 

        <header>
            <h1><a href ="/" onClick={function(e){
              console.log(e);
              e.preventDefault();
              this.state.mode = 'welcome';
              alert('hi');
            }}>{this.state.subject.title}</a></h1>
            {this.state.subject.sub}
        </header>

- ๋‹จ์ˆœํžˆ this.state.mode์˜ ๊ฐ’์„ ๋ฐ”๊พธ๋ฉด ์—๋Ÿฌ

 

this.state.mode = 'welcome';

1. ์ด๋ฒคํŠธ๊ฐ€ ํ˜ธ์ถœ๋˜์—ˆ์„ ๋•Œ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜ ์•ˆ์—์„œ๋Š” this์˜ ๊ฐ’์ด component ์ž๊ธฐ ์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚ค์ง€ ์•Š๊ณ  ์•„๋ฌด ๊ฐ’๋„ ์•„๋‹˜

2. ๋ฆฌ์•กํŠธ๊ฐ€ state์˜ ๊ฐ’์ด ๋ฐ”๋€Œ๋Š” ๊ฒƒ์„ ๋ชจ๋ฆ„

 

        <header>
            <h1><a href ="/" onClick={function(e){
              console.log(e);
              e.preventDefault();
              // this.state.mode = 'welcome';
              this.setState({
                mode:'welcome'
              });
              alert('hi');
            }.bind(this)}>{this.state.subject.title}</a></h1>
            {this.state.subject.sub}
        </header>

ํ•ด๊ฒฐ์ฑ…

1. bind.this๋ฅผ ์ถ”๊ฐ€ํ•ด ์ € ํ•จ์ˆ˜ ๋ฐ”๋กœ ๋’ค์— this๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ํ•จ

2. setState() ์ถ”๊ฐ€

 

React - 16.4. ์ด๋ฒคํŠธ bind ํ•จ์ˆ˜ ์ดํ•ดํ•˜๊ธฐ

- render ํ•จ์ˆ˜ ์•ˆ์—์„œ this๋Š” render ํ•จ์ˆ˜๊ฐ€ ๋ถ™์–ด์žˆ๋Š” component ์ž์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š”๋ฐ ์ด๋ฒคํŠธ ํ˜ธ์ถœ ํ•จ์ˆ˜์—์„œ๋Š” this๊ฐ€ ์•„๋ฌด ๊ฐ’๋„ ๊ฐ€์ง€์ง€ ์•Š์Œ

- bind ํ•จ์ˆ˜๋Š” ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋ณต์‚ฌํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•จ

 

React - 16.5. ์ด๋ฒคํŠธ setState ํ•จ์ˆ˜ ์ดํ•ดํ•˜๊ธฐ
this.state.mode = 'welcome';

- ๋ฆฌ์•กํŠธ ์ž…์žฅ์—์„œ๋Š” ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๊ฐ’์ด ๋ฐ”๋€Œ์—ˆ๋Š”์ง€ ์ธ์‹ํ•˜์ง€ ๋ชปํ•จ

- ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜์–ด ๋งŽ์€ ์ผ์„ ํ•˜๋„๋ก ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ฒƒ์ด setState ํ•จ์ˆ˜์ž„

- state ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด setState ํ•จ์ˆ˜๋ฅผ ์จ์ฃผ์–ด์•ผ ํ•จ

- constructor์—์„œ๋Š” ํŽธํ•˜๊ฒŒ state๋ฅผ ๋ณ€๊ฒฝํ•ด์ฃผ์–ด๋„ ์ƒ๊ด€์Œ

Comments