ํฐ์คํ ๋ฆฌ ๋ทฐ
[React Study] ์ํ์ฝ๋ฉ React - 16. ์ด๋ฒคํธ state props ๊ทธ๋ฆฌ๊ณ render ํจ์ ~ 16.5. ์ด๋ฒคํธ setState ํจ์ ์ดํดํ๊ธฐ
๊ตฌ๋์ 2022. 4. 4. 01:18
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๋ฅผ ๋ณ๊ฒฝํด์ฃผ์ด๋ ์๊ด์
'๐ STUDY > ๐ React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
- MySQL
- ๋ฒํผ
- Programming
- ๋ค์คํ๋ก์ธ์ค
- Apache
- Baekjoon
- machine learning
- bitnami
- ํ์ผ ์ ์ถ๋ ฅ
- Annotation
- C
- SWiFT
- 9086๋ฒ
- ํ๋ก์ธ์ค
- Linux
- error
- ์๊ณ ๋ฆฌ์ฆ
- ํ๋ก๊ทธ๋๋ฐ
- ๋ฐฑ์ค
- PHP
- GIT
- react
- react-scripts
- ์ค์น
- ์ค๋ฅ
- ๋ฆฌ๋ ์ค
- ๊ธฐ๊ณํ์ต
- ๋จธ์ ๋ฌ๋
- C++
- SpringBoot
- Total
- Today
- Yesterday