๐ React ์ ๋ฌธ 06 - State
๐ React ์
๋ฌธ 06 - State
๐
ใ์ํ์ ์ฒ์ ๋ง๋ ๋ฆฌ์กํธใ๋ฅผ ์ฝ๊ณ ์ ๋ฆฌํ ๊ธ์ ๋๋ค.
State๋?
React์ ์์ ์ฃผ๊ธฐ๋ฅผ ์ดํดํ๊ธฐ ์ํด์๋State์ ๋ํด์ ์์์ผ ํ๋ค.State๋React Component์ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ๋ฐ์ดํฐ์ด๋ฉฐ,State๊ฐ ๋ณ๊ฒฝ๋๋ฉดComponent๊ฐ ๋ค์ ๋ ๋๋ง ๋๋ค.- ๊ทธ๋ ๊ธฐ ๋๋ฌธ์
State๋ฅผ ์ ์ํ ๋๋ ๊ผญ ๋ ๋๋ง์ด๋ ๋ฐ์ดํฐ ํ๋ฆ์ ์ฌ์ฉ๋๋ ๊ฐ๋ง ํฌํจ์์ผ์ผ ํ๋ค.ย ย State๋ฅผ ๋ฌด๋ถ๋ณํ๊ฒ ์ ์ํ๋ค๋ฉด ์ ํ๋ฆฌ์ผ์ด์ ์ ์ฑ๋ฅ์ ์ ํ ์ํฌ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.State๋ ๋ฐ๋ก ๋ณต์กํ ํํ๊ฐ ์๋ ๊ฒ ์๋๋ผ ํ๋์Javascript๊ฐ์ฒด์ด๋ค.
1
2
3
4
5
6
7
8
9
class LikeButton extends React.Component {
constructor(props) {
super(props);
this.state = {
liked : false
};
}
}
Class Component์ ๊ฒฝ์ฐ ์ํ๋ ์์ฑ์ ์ฝ๋ ๋ด์ ์ ์๋์ด ์๋ค.
1
2
3
4
5
6
7
8
9
// ์ง์ ์์
this.state = {
name : "kim"
}
// setState ํจ์๋ฅผ ํตํด ์์
this.setState({
name : "kim"
});
State๋ ์ผ๋ฐ์ ์ธJavascript๋ณ์๋ฅผ ๋ค๋ฃจ๋ฏ ์์ ํ๋ฉด ์ ๋๋ค.State๋Component์ ๋ ๋๋ง๊ณผ ๊ด๋ จ ์๊ธฐ ๋๋ฌธ์ ๋ง์๋๋ก ์์ ํ๊ฒ ๋๋ฉด ์ ํ๋ฆฌ์ผ์ด์ ์ด ๊ฐ๋ฐ์๊ฐ ์๋ํ ๋๋ก ๋์ํ์ง ์์ ์ ์๋ค.State๋ฅผ ๋ณ๊ฒฝํ๊ณ ์ ํ ๋์๋ ๊ผญsetState()ํจ์๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
ํ๊ณ
- ๋ด์ฉ์ ์ ๋ฆฌํ๋ฉฐ
React์์ ๋ฐ์ดํฐ ํ๋ฆ๊ณผUI๋ ๋๋ง ๋ฐฉ์์ ๋ํ ์ดํด๋ฅผ ํ ์ ์์๋ค. - ์ง๊ธ๊น์ง๋ ํ์ํ ๊ฐ์ ์ ๋ถ
state์ ๋ฃ๋ ์ต๊ด์ด ์์๋๋ฐ, ๊ผญ ๋ ๋๋ง์ ์ํฅ์ ์ฃผ๋ ๊ฐ๋ง ํฌํจํด์ผ ํ๋ค๋ ๊ฑธ ๋ฐฐ์ธ ์ ์์๋ค.
This post is licensed under CC BY 4.0 by the author.
