ํฐ์คํ ๋ฆฌ ๋ทฐ

์ด์ ๋ด์ฉ ๋ณต์ต
[WEB] - [React] ๊ณต์ ๋ฌธ์ 3. ์๋ฆฌ๋จผํธ ๋ ๋๋ง
[React] ๊ณต์ ๋ฌธ์ 3. ์๋ฆฌ๋จผํธ ๋ ๋๋ง
3. ์๋ฆฌ๋จผํธ ๋ ๋๋ง ์๋ฆฌ๋จผํธ React ์ฑ์ ๊ฐ์ฅ ์์ ๋จ์ ์ผ๋ฐ ๊ฐ์ฒด React DOM์ React ์๋ฆฌ๋จผํธ์ ์ผ์นํ๋๋ก DOM์ ์ ๋ฐ์ดํธ ๋ถ๋ณ ๊ฐ์ฒด, ์๋ฆฌ๋จผํธ ์์ฑ ์ดํ ํด๋น ์๋ฆฌ๋จผํธ์ ์์์ด๋ ์์ฑ ๋ณ๊ฒฝ ๋ถ๊ฐ
nakyungim.tistory.com
- UI๋ฅผ ์
๋ฐ์ดํธ ํ๋ ํ ๊ฐ์ง ๋ฐฉ๋ฒ
- ์๋ก์ด ์๋ฆฌ๋จผํธ๋ฅผ ์์ฑ ํ ์ด๋ฅผ ReactDOM.render()๋ก ์ ๋ฌ
- ๋ ๋๋ง ๋ ์ถ๋ ฅ๊ฐ์ ๋ณ๊ฒฝํ๊ธฐ ์ํด ReactDOM.render() ํธ์ถ
5. State์ ์๋ช ์ฃผ๊ธฐ
๋ชฉํ :
1. Clock ์ปดํฌ๋ํธ๋ฅผ ์์ ํ ์ฌ์ฌ์ฉํ๊ณ ์บก์ํ ํ๊ธฐ
2. ์ปดํฌ๋ํธ๊ฐ ์ค์ค๋ก ํ์ด๋จธ ์ค์ and ๋งค์ด ์ค์ค๋ก ์ ๋ฐ์ดํธํ๊ธฐ
์๋ index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
function tick(){
const element = (
<div>
<h1>Hello, world!</h1>
<h2>It is {new Date().toLocaleTimeString()}.</h2>
</div>
);
ReactDOM.render(
element,
document.getElementById('root')
);
}
setInterval(tick, 1000);
1. Clock ์ปดํฌ๋ํธ๋ฅผ ์์ ํ ์ฌ์ฌ์ฉํ๊ณ ์บก์ํ ํ๊ธฐ
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
function Clock(props){
return(
<div>
<h1>Hello, world!</h1>
<h2>It is {props.date.toLocaleTimeString()}.</h2>
</div>
);
}
function tick(){
ReactDOM.render(
<Clock date={new Date()} />,
document.getElementById('root')
);
}
setInterval(tick, 1000);
2. ์ปดํฌ๋ํธ๊ฐ ์ค์ค๋ก ํ์ด๋จธ ์ค์ and ๋งค์ด ์ค์ค๋ก ์ ๋ฐ์ดํธํ๊ธฐ
-> Clock ์ปดํฌ๋ํธ์ state ์ถ๊ฐ
ํจ์์์ ํด๋์ค๋ก ๋ณํํ๊ธฐ
ํจ์ ์ปดํฌ๋ํธ -> ํด๋์ค ์ปดํฌ๋ํธ ๋ณํ 5๋จ๊ณ
1. React.Component๋ฅผ ํ์ฅํ๋ ๋์ผํ ์ด๋ฆ์ ES6 class๋ฅผ ์์ฑํฉ๋๋ค.
-> Clock.js ์์ฑ
2. render()๋ผ๊ณ ๋ถ๋ฆฌ๋ ๋น ๋ฉ์๋๋ฅผ ์ถ๊ฐํฉ๋๋ค.
-> function Clock(props)์ class Clock extends Component๋ก ๋ณ๊ฒฝ
-> ํด๋์ค ๋ด๋ถ์ render() ๋ฉ์๋ ์ถ๊ฐ
3. ํจ์์ ๋ด์ฉ์ render() ๋ฉ์๋ ์์ผ๋ก ์ฎ๊น๋๋ค.
-> ํจ์ ๋ด๋ถ ๋ด์ฉ์ render() ๋ด๋ถ๋ก ์ด๋
4. render() ๋ด์ฉ ์์ ์๋ props๋ฅผ this.props๋ก ๋ณ๊ฒฝํฉ๋๋ค.
-> props ์์ this. ์ถ๊ฐ
5. ๋จ์์๋ ๋น ํจ์ ์ ์ธ์ ์ญ์ ํฉ๋๋ค.
<Clock.js>
import React from 'react';
class Clock extends React.Component{
render(){
return(
<div>
<h1>Hello, world!</h1>
<h2>It is {this.props.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
export default Clock;
- render() ๋ฉ์๋๋ ์ ๋ฐ์ดํธ๊ฐ ๋ฐ์ํ ๋๋ง๋ค ํธ์ถ
- ๊ฐ์ DOM ๋
ธ๋๋ก <Clock />์ ๋ ๋๋งํ๋ ๊ฒฝ์ฐ, Clock ํด๋์ค์ ๋จ์ผ ์ธ์คํด์ค๋ง ์ฌ์ฉ
-> ๋ก์ปฌ state์ ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋์ ๊ฐ์ ๋ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ ์ฌ์ฉ ๊ฐ๋ฅํ๊ฒ ํจ
ํด๋์ค์ ๋ก์ปฌ State ์ถ๊ฐํ๊ธฐ
date๋ฅผ props์์ state๋ก ์ด๋ํ๊ธฐ 3๋จ๊ณ
1. render() ๋ฉ์๋ ์์ ์๋ this.props.date๋ฅผ this.state.date๋ก ๋ณ๊ฒฝํฉ๋๋ค.
-> props๋ฅผ state๋ก ๋ณ๊ฒฝ
2. ์ด๊ธฐ this.state๋ฅผ ์ง์ ํ๋ class constructor๋ฅผ ์ถ๊ฐํฉ๋๋ค.
constructor(props){
super(props);
this.state = {date: new Date()};
} // ์ด๊ธฐ state ์ง์
- ํด๋์ค ์ปดํฌ๋ํธ๋ ํญ์ props๋ก ๊ธฐ๋ณธ ์์ฑ์ ํธ์ถ
3. <Clock /> ์์์์ date prop์ ์ญ์ ํฉ๋๋ค.
<Clock.js>
import React from 'react';
class Clock extends React.Component{
constructor(props){
super(props);
this.state = {date: new Date()};
} // ์ด๊ธฐ state ์ง์
render(){
return(
<div>
<h1>Hello, world!</h1>
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
export default Clock;
<index.js>
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import Clock from './Clock';
function tick(){
ReactDOM.render(
<Clock />,
document.getElementById('root')
);
}
setInterval(tick, 1000);
=> ์ฌ๊ธฐ๊น์ง ์๋ฃํ๊ณ ๋ ๋๋งํ๋ฉด ์ด ์ ๋ฐ์ดํธ๊ฐ ์๋๋ ๊ฒ์ด ๋ง์! ์ด์ ๋ถํฐ ํ ๊ฑฐ์์~
์๋ช ์ฃผ๊ธฐ ๋ฉ์๋๋ฅผ ํด๋์ค์ ์ถ๊ฐํ๊ธฐ
๋ง์ดํ : Clock์ด ์ฒ์ DOM์ ๋ ๋๋ง ๋ ๋๋ง๋ค ํ์ด๋จธ๋ฅผ ์ค์ ํ๋ค.
์ธ๋ง์ดํ : Clock์ ์ํด ์์ฑ๋ DOM์ด ์ญ์ ๋ ๋๋ง๋ค ํ์ด๋จธ๋ฅผ ํด์ ํ๋ค.
๋ง์ ์ปดํฌ๋ํธ๊ฐ ์๋ ์ดํ๋ฆฌ์ผ์ด์ ์์ ์ปดํฌ๋ํธ๊ฐ ์ญ์ ๋ ๋ ํด๋น ์ปดํฌ๋ํธ๊ฐ ์ฌ์ฉ ์ค์ด๋ ๋ฆฌ์์ค๋ฅผ ํ๋ณดํ๋ ๊ฒ์ด ์ค์!
์๋ช ์ฃผ๊ธฐ ๋ฉ์๋ ์ ์ธํ์ฌ ์ปดํฌ๋ํธ๊ฐ ๋ง์ดํธ or ์ธ๋ง์ดํธ ๋ ๋ ์ผ๋ถ ์ฝ๋๋ฅผ ์๋ํ ์ ์๋ค.
์ด ์ฝ๋์์ ์ฌ์ฉ๋ ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋๋
constructor
componentDidMount
componentWillUnmount
render
<Clock.js>
import React from 'react';
class Clock extends React.Component{
constructor(props){
super(props);
this.state = {date: new Date()};
} // ์ด๊ธฐ state ์ง์
componentDidMount(){
this.timerID = setInterval(
() => this.tick(),
1000
) // ํ์ด๋จธ ์ค์ (timerID ์ ์ฅ)
} // ์ปดํฌ๋ํธ ์ถ๋ ฅ๋ฌผ์ด DOM์ ๋ ๋๋ง ๋ ํ์ ์คํ
componentWillUnmount(){
clearInterval(this.timerID);
} // ํ์ด๋จธ ๋ถํด
tick() {
this.setState({
date: new Date()
}); // setState๋ ์ปดํฌ๋ํธ ๋ก์ปฌ state ์
๋ฐ์ดํธ
} // Clock ์ปดํฌ๋ํธ๊ฐ ๋งค์ด ์๋ํ๋๋ก ํ๋ ๋ฉ์๋
render(){
return(
<div>
<h1>Hello, world!</h1>
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
export default Clock;
State๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์ฌ์ฉํ๊ธฐ
setState() ์ ๋ํด์ ์์์ผ ํ 3๊ฐ์ง
1. ์ง์ State๋ฅผ ์์ ํ์ง ๋ง๊ธฐ -> ๋์ setState() ์ฌ์ฉ!
this.state.comment = 'Hi'; // Wrong
this.setState({comment: 'Hi'}); // Correct
* this.state๋ฅผ ์ง์ ํ ์ ์๋ ์ ์ผํ ๊ณต๊ฐ์ constructor
2. State ์ ๋ฐ์ดํธ๋ ๋น๋๊ธฐ์ ์ผ ์๋ ์๋ค.
// Wrong
this.setState({
counter: this.state.counter + this.props.increment,
});
// Correct (arrow function ์ฌ์ฉ)
this.setState((state, props) => ({
counter: state.counter + props.increment
}));
// Correct (arrow function ์ฌ์ฉx)
this.setState(function(state, props) {
return {
counter: state.counter + props.increment
};
});
3. State ์ ๋ฐ์ดํธ๋ ๋ณํฉ๋๋ค.
state๋ ๋ค์ํ ๋ ๋ฆฝ๋ณ์๋ฅผ ํฌํจํ ์ ์์
constructor(props) {
super(props);
this.state = {
posts: [],
comments: []
};
}
๋ณ๋์ setState() ํธ์ถ๋ก ๋ณ์๋ฅผ ๋ ๋ฆฝ์ ์ผ๋ก ์ ๋ฐ์ดํธ ๊ฐ๋ฅ
componentDidMount() {
fetchPosts().then(response => {
this.setState({
posts: response.posts
});
});
fetchComments().then(response => {
this.setState({
comments: response.comments
});
});
}
๋ณํฉ์ ์๊ฒ ์ด๋ฃจ์ด์ง๊ธฐ ๋๋ฌธ์ this.setState({comments})๋ this.state.posts์ ์ํฅ์ ์ฃผ์ง ์์ง๋ง this.state.comments๋ ์์ ํ ๋์ฒด๋๋ค.
The Data Flows Down (๋ฐ์ดํฐ๋ฅผ ์๋๋ก ํ๋ฅธ๋ค.)
์ปดํฌ๋ํธ๋ ์์ ์ state๋ฅผ ์์ ์ปดํฌ๋ํธ์ props๋ก ์ ๋ฌํ ์ ์๋ค.
"ํํฅ์(top-down)" ๋๋ "๋จ๋ฐฉํฅ์" ๋ฐ์ดํฐ ํ๋ฆ
์ ์ฒด ์ฝ๋ ๋งํฌ
github.com/NakyungIm/React/tree/master/clock-app
NakyungIm/React
Contribute to NakyungIm/React development by creating an account on GitHub.
github.com
- Total
- Today
- Yesterday
- ํด๋ผ์ฐ๋
- ๋ฆฌ๋์ค
- ๋จธ์ ๋ฌ๋
- ๊ตฌ๊ธ
- ๋ฆฌ์กํธ ์ปดํฌ๋ํธ
- React DOM
- ๋ฆฌ์กํธ ๊ธฐ์ด
- GCP
- ๋ฆฌ์กํธ ๋ ๋๋ง
- React Hook
- Docker&Kubernetes:์ค์ ๊ฐ์ด๋-2022๋ ํ
- ๋ฆฌ์กํธ useEffect
- ๋์ปค
- ๋ฆฌ์กํธ setState
- docker
- ๋ฆฌ์กํธ
- ML
- React
- ๋ฆฌ์กํธ ํ
- ๊ตฌ๊ธํด๋ผ์ฐ๋ํ๋ซํผ
- ์ํ์ฝ๋ฉ ๋ฆฌ์กํธ
- ๋จ๊ธฐ์ง์ค๊ณผ์
- ํ ์ํ๋ก์ฐ
- machine learning
- ๋ฆฌ์กํธ state
- Cloud
- ๋ฆฌ์กํธ ๊ณต์๋ฌธ์
- ๋ฆฌ๋์ค store
- ์ปดํฌ๋ํธ
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |