Front-End μž‘μ—…μ‹€/Vue.js

[Reacte] Event setState ν•¨μˆ˜

μ£Όλ‹ˆμ“°πŸ§‘‍πŸ’» 2021. 10. 2. 08:01
728x90
λ°˜μ‘ν˜•

 

 

 

πŸ“Œ Event setState ν•¨μˆ˜ μ΄ν•΄ν•˜κΈ°


λ°˜μ‘ν˜•
return (
            <div className="App">
                {/* Stateν™” ν•˜κΈ° μ „ μ½”λ“œ */}
                {/*<Subject title="WEB" sub="word wide web!"></Subject>*/}

                {/*Stateν™” ν•œ λ’€ μ½”λ“œ*/}
                {/*<Subject title={this.state.subject.title} sub={this.state.subject.sub}></Subject>*/}

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

                <Nav data = {this.state.contents}> </Nav>
                <Content title={_title} desc={_desc}> </Content>
            </div>

 

μœ„μ™€ κ°™μ€ μ½”λ“œμ—μ„œ μ»΄ν¬λ„ŒνŠΈκ°€ μ΄λ―Έ μƒμ„±λœ μ΄ν›„에 [this.state.mode = 'welcome']처럼 State κ°’을 μ§μ ‘ λ³€κ²½ν•˜λ©΄ μ•ˆλ˜κ³ , ν•¨μˆ˜ ν˜•νƒœλ‘œ λ³€κ²½ν•΄μ•Ό ν•˜λŠ” μ΄μœ λ₯Ό μ•Œμ•„λ³Ό κ²ƒμ΄μ—μš”.

 

 constructor(props) {
        // State 값을 μ΄ˆκΈ°ν™” μ‹œν‚€κΈ° μœ„ν•¨μ΄κ³ , κ·Έ μ΄ˆκΈ°κ°’μœΌλ‘œ 'title="WEB" sub="word wide web!"'을 μ„€μ •ν•˜λ € ν•œλ‹€.
        super(props);
        this.state = {
            mode : 'read',
            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: 'CSS is for desing'},
                {id:3, title:'JavaScript', desc: 'JavaScript is for interative'}
            ]
        }
    } // constructor() 끝

 

λ¨Όμ € μœ„μ—μ„œ μƒμ„±μžκ°€ Appμ΄λΌλŠ” μ»΄ν¬λ„ŒνŠΈκ°€ μƒμ„±λ  λ•Œ κ°€μž₯ λ¨Όμ € μ‹€ν–‰λ˜λŠ” 
Constructorμ—μ„œλŠ” this.state = { ... }와 κ°™μ΄ μ΄λŸ°μ‹μœΌλ‘œ μ„€μ •ν•˜λ©΄ λ˜λŠ” κ²ƒμ΄μ—μš”.

ν•˜μ§€λ§Œ, μ΄λ―Έ μ»΄ν¬λ„ŒνŠΈκ°€ μƒμ„±μ΄ λλ‚œ λ’€μ— λ™μ μœΌλ‘œ State κ°’을 λ°”κΎΈκ³ μž ν•  λ•ŒλŠ” μœ„와 κ°™μ΄ ν•˜λ©΄ μ•ˆλœλ‹΅λ‹ˆλ‹€!


그것이 λ°”λ‘œ [this.state.mode = 'welcome']κ³Ό κ°™μ΄ ν•˜λŠ” κ²ƒμ΄μ—μš”.
μ΄λ ‡κ²Œ ν•˜λ©΄ μ•ˆλ˜λŠ” κ²ƒμ΄μ—μš”.

이럴 λ•Œ this.setState()에 Body뢀에 λ‚΄κ°€ μ„€μ •ν•˜κ³ μž ν•˜λŠ” λ‚΄μš©μ„ κ°μ²΄ ν˜•νƒœλ‘œ μ€˜μ•Ό ν•œλ‹΅λ‹ˆλ‹€!

κ·Έ μ΄μœ λŠ” λ¬΄μ—‡μΌκΉŒ? λ¦¬μ—‘νŠΈ μž…μž₯μ—μ„œλŠ” [this.state.mode = 'welcome']처럼 λ°”꿔버리면 λ¦¬μ—‘νŠΈ λͺ¨λ₯΄κ²Œ λͺ°λž˜ λ°”꾼것과 κ°™μ€ κ²ƒμ΄μ—μš”.
κ·Έλž˜μ„œ λͺ¨λ₯΄λŠ” κ±°λ‹ŒκΉŒ λžœλ”링을 ν•  μˆ˜ μ—†λŠ” κ²ƒμ΄μ—μš”.

κ·Έλ ‡κΈ° λ•Œλ¬Έμ— State κ°’이 λ°”λ€Œλ©΄ setStateλ₯Ό μ¨μ•Όν•œλ‹΅λ‹ˆλ‹€!

 

 


 

μ£Όλ‹ˆν•˜λž‘μ˜ 글이 λ§ˆμŒμ— λ“œμ…¨λ‚˜μš”? ꡬ독과 곡감! 그리고, λŒ“κΈ€ 그리고 λ°©λͺ…둝은 μ£Όλ‹ˆν•˜λž‘μ—κ²Œ λ§Žμ€ 힘이 λ©λ‹ˆλ‹€.

 

728x90
λ°˜μ‘ν˜•