β‘£ state

hhkimΒ·2019λ…„ 11μ›” 16일
0

μƒν™œμ½”λ”© React

λͺ©λ‘ 보기
4/8
post-thumbnail

πŸ”— μƒν™œμ½”λ”© React

1. State μ†Œκ°œ

  • props
    - μ‚¬μš©μžκ°€ μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•˜λŠ” μž…μž₯μ—μ„œ μ€‘μš”

    • μ»΄ν¬λ„ŒνŠΈμ˜ λ™μž‘μ„ λ°”κΎΈκ³  싢을 λ•Œ μ œκ³΅ν•˜λŠ” 문법적인 정보
  • state
    - props의 값에 따라 λ‚΄λΆ€ κ΅¬ν˜„μ— ν•„μš”ν•œ κ°’

    πŸ‘‰ stateλŠ” λ‹€μ–‘ν•œ λ™μž‘μ„ ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈ μ œμž‘μ— ν•„μˆ˜

    2. State μ‚¬μš©

  • μ™ΈλΆ€μ—μ„œ μ•Œ ν•„μš”κ°€ μ—†λŠ” 정보λ₯Ό 숨길 λ•Œ μ‚¬μš©

// App.js
class App extends Component {
  constructor(props){  // render보닀 λ¨Όμ € μ‹€ν–‰λ˜μ–΄ μ»΄ν¬λ„ŒνŠΈ μ΄ˆκΈ°ν™”
    super(props);
    this.state = {
      subject:{title: 'WEB', sub: 'World Wide Web!'}
    }
  }
  render() {
    return (
      <div className="App">
        // state μ‚¬μš©
        <Subject 
          title={this.state.subject.title} 
          sub={this.state.subject.sub}>
        </Subject>
      </div>
    );
  }
}
  • μƒμœ„ μ»΄ν¬λ„ŒνŠΈμΈ App의 값을 ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈμΈ Subject에 state 값을 톡해 전달 κ°€λŠ₯

3. key

  • state μ‚¬μš©ν•΄μ„œ TOC λ¦¬νŒ©ν† λ§
// App.js
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'}
      ]
    }
  }
  render() {
    return (
      <div className="App">
        ...
        <TOC data={this.state.contents}></TOC>
      </div>
    );
  }
}
// TOC.js
class TOC extends Component {
    render(){
      var lists = [];
      var data = this.props.data;
      var i = 0;
      while(i < data.length){
        lists.push(<li><a href={"/content/"+data[i].id}>{data[i].title}</a></li>);
        i = i + 1;
      }
      return (
        <nav>
            <ul>
                {lists}
            </ul>
        </nav>
      );
    }
  }
  • 각 list 값에 ꡬ뢄이 ν•„μš”ν•˜κΈ° λ•Œλ¬Έμ΄ κ³ μœ κ°’μΈ key λΆ€μ—¬
lists.push(<li key={data[i].id}><a href={"/content/"+data[i].id}>{data[i].title}</a></li>);

0개의 λŒ“κΈ€