์ ํ๋ฆฌ์ผ์ด์ ์ ์ญ๋์ ์ผ๋ก ๋ง๋ค์ด์ค ๊ธฐ์ ์ธ event๋ฅผ ์ดํด๋ณผ ๊ฒ์ด๋ค.
props, state, event ์ด ์ธ ๊ฐ๊ฐ ์ํธ์์ฉํ๋ฉด์ ์น ํ์ด์ง๋ฅผ ์ญ๋์ ์ผ๋ก ๋ง๋ ๋ค.
๋งํฌ์ ์ด๋ฒคํธ๋ฅผ ์ค์นํด์ ํด๋ฆญํ๋ฉด ๊ทธ์ ๋ฐ๋ผ์ App ์ปดํฌ๋ํธ์ state๊ฐ ๋ฐ๋๊ณ
๋ฐ๋ state๊ฐ ์ด content ์ปดํฌ๋ํธ์ props ๊ฐ์ผ๋ก ์ ๋ฌ๋๋ฉด์ ๋์ ์ผ๋ก ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ฐ๊พผ๋ค.
ํ์ฌ ์ฐ๋ฆฌ๊ฐ ๋ณด๊ณ ์๋ ํ์ด์ง์ ์ํ๋ฅผ ๊ตฌ๋ถํ๊ธฐ ์ํด์ state์ mode๋ผ๋ ๊ฐ์ ์ฃผ์๋ค.
๊ธฐ๋ณธ๊ฐ์ผ๋ก welcome์ ์ฃผ์๊ณ welcome ๋ชจ๋์ผ ๋ content ์์ญ์ ํ์ํ ํ ์คํธ๋ ์ง์ ํด์ฃผ์๋ค.
๋ฆฌ์กํธ์์๋ state ๊ฐ์ด ๋ฐ๋๋ฉด ๊ทธ state๋ฅผ ๊ฐ์ง๊ณ ์๋ ์ปดํฌ๋ํธ์ renderํจ์๊ฐ ๋ค์ ํธ์ถ๋๋ค.
๋ค์ ํธ์ถ๋๋ฉด์ render ํจ์ ํ์์ ์ปดํฌ๋ํธ๋ค๋ ๊ฐ์ ๊ฐ์ง๊ณ ์๋ renderํจ์๋ ๋ชจ๋ ๋ค์ ํธ์ถ๋๋ค.
=> ํ๋ฉด์ด ๋ค์ ๊ทธ๋ ค์ง๋ ๊ฒ
์ฆ, render ํจ์๊ฐ ํ๋ ์ผ์ ์ด๋ค html์ ๊ทธ๋ฆด ๊ฒ์ธ๊ฐ. ๋ฅผ ๊ฒฐ์ ํ๋ ํจ์์ด๋ค.
๋ฆฌ์กํธ์์๋ props ๊ฐ์ด๋ state ๊ฐ์ด ๋ฐ๋๋ฉด ํด๋น๋๋ ์ปดํฌ๋ํธ์ render ํจ์๊ฐ ํธ์ถ๋๋๋ก ์ฝ์๋์ด์๋ค.
์ด ๋ชจ๋ ๊ฐ์ ๋ฐ๋ผ์ ๋ง๋ค์ด์ง๋ ์ปดํฌ๋ํธ(๋ง๋ค์ด์ง๋ rendering ๊ฒฐ๊ณผ)๊ฐ ๋ฌ๋ผ์ง๋๋ก ์กฐ๊ฑด๋ฌธ์ ์ฌ์ฉํ๋ค.
๋ฆฌ์กํธ๋ ๊ท์น์ด ์๋ค.
์ง๊ธ ์ง๊ณ ์๋ ์ฝ๋๋ ์ ์ฌhtml์ด๋ค.
onclick์ด ์๋๋ผ onClick์ ์จ์ฃผ์ด์ผํ๊ณ ์ค๊ดํธ๋ก ์คํ๋ ์ฝ๋๋ฅผ ๋ฌถ์ด์ฃผ์ด์ผํ๋ค.
ํ ์คํธ๋ก alert์ฐฝ์ ๋์๋ณด์๋๋ฐ alert ์ฐฝ ํ์ธ์ ๋๋ฌ์ฃผ๋ฉด ํ์ด์ง๊ฐ reload ๋์ด๋ฒ๋ฆฐ๋ค.
๋ฆฌ์กํธ์ reload ํ์ง ์๊ณ ๋ ์ญ๋์ ์ธ ์ฌ์ดํธ๋ฅผ ๋ง๋ค ์ ์๋ค๋ ์ฅ์ ์ด ์์ด์ง๋ค.
aํ๊ทธ๋ ํด๋ฆญํ์ ๋ href๊ฐ ๊ฐ๋ฆฌํค๋ ๊ณณ์ผ๋ก ์ด๋ํ๋ค๋ ๊ธฐ๋ณธ์ ์ธ ๋์์ด ์๋๋ฐ,
์ด ๊ธฐ๋ณธ๋์์ ๋ชปํ๊ฒ ํ๊ณ ์ถ์ ๊ฒ!!
์ด๋ป๊ฒ ํ๋?
ํจ์๊ฐ ์คํ๋ ๋ ๋ฆฌ์กํธ๋ ํจ์์ ์ฒซ๋ฒ์งธ ํ๋ผ๋ฏธํฐ ๊ฐ์ผ๋ก ์ด๋ฒคํธ๋ผ๋ ๊ฐ์ฒด๋ฅผ ์ฃผ์ ํด์ฃผ๊ธฐ๋ก ์ฝ์๋์ด์๋ค.
console.log๋ก ํ์ธํด๋ณด์๋ค.
debugger; ๋ผ๋ ์ฝ๋๋ ํฌ๋กฌ ๊ฐ๋ฐ์ ๋๊ตฌ๋ฅผ ์ธ ๋, ํฌ๋กฌ ๊ฐ๋ฐ์ ๋๊ตฌ๋ฅผ ์ผ๋์ผ๋ฉด ๋ธ๋ผ์ฐ์ ๊ฐ debugger๋ผ๊ณ ๋์ด์๋ ๋ถ๋ถ์์ ์คํ์ ๋ฉ์ถ๊ณ sources๋ผ๋ ํ์ด์ง๋ก ์ด๋ํด์ ์ฐ๋ฆฌ๊ฐ ์ฌ๋ฌ ์ ๋ณด๋ฅผ ๋ณด๊ธฐ ์ฝ๊ฒ ํด์ค๋ค.
debugger์์ ๋ฉ์ถ ๊ฒ์ ๋ณผ ์ ์๋ค.
์ด ๋ ํธ์ถ๋ ํจ์์ ์ฒซ ๋ฒ์จฐ ์ธ์๋ก e๋ฅผ ์คฌ๋๋ฐ
e๋ ์ฐ๋ฆฌ๊ฐ ํธ๋ค๋งํ ์ ์๊ฒ ์ฐ๋ฆฌ์๊ฒ ์ฃผ์ ๋๋ ์ ๋ณด์ด๋ค.
html ํ๊ทธ์ ๊ธฐ๋ณธ๋์์ ๋ชปํ๊ฒ ๋ง๋ ๋ฐฉ๋ฒ์ด ์๋ ์ฝ๋์ด๋ค.
e.preventDefault();
render() ํจ์
import './App.css';
import React, { Component } from 'react';
import TOC from "./components/TOC"
import Content from "./components/Content"
import Subject from "./components/Subject"
// ์ปดํฌ๋ํธ๋ฅผ ๋ง๋๋ ์ฝ๋ (ํ
ํ๋ฆฟ)
// ๋ฆฌ์กํธ์ Component๋ผ๊ณ ํ๋ ํด๋์ค๋ฅผ ์์ํ๋
// App ์ด๋ผ๋ ํด๋์ค - render()๋ผ๋ ํจ์๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
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 HyperText ...'},
{id:2, title:'CSS', desc:'CSS is for design ...'},
{id:3, title:'JavaSript', desc:'JavaSript is ...'}
]
}
}
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> */}
<header>
<h1><a href="/" onClick={function(e){
//alert('hi');
console.log(e);
e.preventDefault();
}}>{this.state.subject.title}</a></h1>
{this.state.subject.sub}
</header>
<TOC data={this.state.contents}></TOC>
<Content title={_title} desc={_desc}></Content>
</div>
);
}
}
export default App;
'WEB > Front-End๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] ์ด๋ฒคํธ, state ํ๊ณ ๋ค๊ธฐ (0) | 2021.10.07 |
---|---|
[React] event์์ state ๋ณ๊ฒฝ (feat. bind, setState) (0) | 2021.10.07 |
[React] state๋? (props์ ๋น๊ต) (0) | 2021.10.02 |
[React] ์ปดํฌ๋ํธ ํ์ผ ๋ถ๋ฆฌ (0) | 2021.10.01 |
[React] Props (0) | 2021.10.01 |