์‚๋‹ˆ๋กœ๊ทธ
hello world!
์‚๋‹ˆ๋กœ๊ทธ
  • hello world! (81)
    • Algorithm (27)
      • Koala ๐Ÿจ (8)
      • Tutorial ๐Ÿค (8)
      • ๋‹จ๊ณ„๋ณ„๋กœ ํ’€์–ด๋ณด๊ธฐ ๐Ÿพ (11)
    • Programmers (35)
      • Level 1 (35)
      • Level 2 (0)
      • Level 3 (0)
    • Study (13)
      • CS50 ๐ŸŒŠ (3)
      • JavaScript ๐Ÿ’ฅ (3)
      • Spring ๐ŸŒฑ (3)
      • React ๐ŸŒฅ (4)
      • Project ๐Ÿš€ (0)
    • ์ทจ๋ฝ€๊ธฐ์› (6)
hELLO ยท Designed By ์ •์ƒ์šฐ.
์‚๋‹ˆ๋กœ๊ทธ

hello world!

state
Study/React ๐ŸŒฅ

state

2022. 7. 20. 12:55

 

state๋ž€?

state๋Š” props์ฒ˜๋Ÿผ App ์ปดํฌ๋„ŒํŠธ์˜ ๋ Œ๋”๋ง ๊ฒฐ๊ณผ๋ฌผ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์ง€๋งŒ,

props๋Š” ์ปดํฌ๋„ŒํŠธ์™€ ์ „๋‹ฌ๋˜๋Š” ๋ฐ˜๋ฉด state๋Š” ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ๊ด€๋ฆฌ๋œ๋‹ค๋Š” ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

state๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š”, ์‚ฌ์šฉํ•˜๋Š” ์ชฝ๊ณผ ๊ตฌํ˜„ํ•˜๋Š” ์ชฝ์„ ์ฒ ์ €ํ•˜๊ฒŒ ๋ถ„๋ฆฌ์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค.

 

state ์˜ˆ์‹œ

src/App.js

import React, { Component } from 'react';
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'}
            ]
        }
    }
  render() {
    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="HTML is HyperText Markup Language."></Content>
        </div>
    );
  }
}

export default App;

 

src/components/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;

 

src/components/TOC.js

import React, {Component} from 'react';

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

        return(
            <nav>
                <ul>
                    {lists}
                </ul>
            </nav>
        );
    }
}

export default TOC;

 

src/components/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;

 

 

์ƒํ™œ์ฝ”๋”ฉ์˜ React๊ฐ•์˜๋ฅผ ๋ณด๊ณ  ์ž‘์„ฑ๋œ ๊ธ€์ž…๋‹ˆ๋‹ค.

์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'Study > React ๐ŸŒฅ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

event  (0) 2022.07.20
์ปดํฌ๋„ŒํŠธ ์ œ์ž‘  (0) 2022.07.14
๊ฐœ๋ฐœ ํ™˜๊ฒฝ  (0) 2022.07.14
    'Study/React ๐ŸŒฅ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • event
    • ์ปดํฌ๋„ŒํŠธ ์ œ์ž‘
    • ๊ฐœ๋ฐœ ํ™˜๊ฒฝ
    ์‚๋‹ˆ๋กœ๊ทธ
    ์‚๋‹ˆ๋กœ๊ทธ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”