props, state - MoonGyeongHyeon/React_Study GitHub Wiki
์ปดํฌ๋ํธ์์ ์ฌ๋ฌ Element๋ฅผ ๋ ๋๋งํด์ผ ํ ๋, ๊ทธ element๋ค์ ํ์์ ์ผ๋ก container element ์์ ํฌํจ์์ผ์ผ ํ๋ค.
return (
<h1> Hello Velopert</h1>
<h2> Welcome </h2>
);
์ ์ฝ๋๋
Module build failed: SyntaxError: /home/vlpt/node_tutorial/react/react-tutorials/03-jsx/src/App.js: Adjacent JSX elements must be wrapped in an enclosing tag (10:12)
์ ๊ฐ์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ฏ๋ก ์๋์ ๊ฐ์ด ์์ ํ๋ค.
return (
<div>
<h1> Hello Velopert </h1>
<h2> Welcome </h2>
</div>
);
์ปดํฌ๋ํธ์์ ์ฌ์ฉํ ๋ฐ์ดํฐ ์ค ๋ณ๋๋์ง ์๋ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃฐ ๋ ์ฌ์ฉ๋๋ค. parent ์ปดํฌ๋ํธ์์ child ์ปดํฌ๋ํธ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ํ ๋ 'props'๊ฐ ์ฌ์ฉ๋๋ค.
-
props ์ถ๊ฐํ๊ธฐ
์ปดํฌ๋ํธ์์ immutable(๋ถ๋ณ) ๋ฐ์ดํฐ๊ฐ ํ์ํ ๋,
render()
ํจ์์ ๋ด๋ถ์{this.props.propsName}
ํ์์ผ๋ก ๋ฃ๊ณ , ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ ๋,< >
๊ดํธ ์์propsName="Value"
๋ฅผ ๋ฃ์ด ๊ฐ์ ์ค์ ํ๋ค. -
์์
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
const rootElement = document.getElementById('root');
ReactDOM.render(<App headerTitle="Welcome!"
contentTitle="Stranger"
contentBody="Welcome to example app"/>, rootElement);
App.js
import React from 'react';
import Header from './Header';
import Content from './Content';
class App extends React.Component {
render(){
return (
<div>
<Header title={this.props.headerTitle}/>
<Content title={this.props.contentTitle} content={this.props.contentBody}/>
</div>
);
}
}
export default App;
Header.js
import React from 'react';
class Header extends React.Component {
render(){
return (
<h1>{this.props.title}</h1>
);
}
}
export default Header;
Content.js
import React from 'react';
class Content extends React.Component {
render(){
return (
<div>
<h2>{this.props.title}</h2>
<p>{this.props.content}</p>
</div>
);
}
}
export default Content;
props ๊ฐ์ ์์๋ก ์ง์ ํ์ง ์์์ ๋ ์ฌ์ฉํ ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํ ์ ์๋ค. ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํ ๋, ์ปดํฌ๋ํธ ํด๋์ค ํ๋จ์ className.defaultProps={propName: value}
๋ฅผ ์ฝ์
ํ๋ฉด ๋๋ค.
-
์์
์ ์์ ์์ App.js ์ ์ฝ๊ฐ์ ์ฝ๋๋ง ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค.
App.js
import React from 'react';
import Header from './Header';
import Content from './Content';
class App extends React.Component {
render(){
return (
<div>
<Header title={this.props.headerTitle}/>
<Content title={this.props.contentTitle} content={this.props.contentBody}/>
</div>
);
}
}
/*
* ์๋์ ๊ฐ์ด ์ถ๊ฐ
*/
App.defaultProps = {
headerTitle: 'Default header',
contentTitle: 'Default contentTitle',
contentBody: 'Default contentBody'
};
export default App;
์ปดํฌ๋ํธ์์ ์ ๋์ ์ธ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃฐ ๋ state
๋ฅผ ์ฌ์ฉํ๋ค. React.js ์ดํ๋ฆฌ์ผ์ด์
์ ๋ง๋ค ๋ state
๋ฅผ ์ฌ์ฉํ๋ ์ปดํฌ๋ํธ์ ๊ฐ์๋ฅผ ์ต์ํํด์ผ ํ๋ค. ์๋ฅผ ๋ค์ด 10๊ฐ์ ์ปดํฌ๋ํธ์์ ์ ๋์ ์ธ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๊ฒ ๋ ๋, ๊ฐ ๋ฐ์ดํฐ์ state๋ฅผ ์ฌ์ฉํ ๊ฒ ์๋๋ผ, props
๋ฅผ ์ฌ์ฉํ๊ณ 10๊ฐ์ ์ปดํฌ๋ํธ๋ฅผ ํฌํจ์ํค๋ container ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ํจ์จ์ ์ด๋ค.
- ์์
App.js
import React from 'react';
import StateExample from "./StateExample";
class App extends React.Component {
render() {
return (
<div>
<StateExample/>
</div>
);
}
}
export default App;
StateExample.js
import React from 'react';
class StateExample extends React.Component {
constructor(props) {
super(props);
this.state = {
header: "Header Initial state",
content: "Content Initial State"
};
}
updateHeader(text){
this.setState({
header: "Header has changed"
});
}
render() {
return (
<div>
<h1>{this.state.header}</h1>
<h2>{this.state.content}</h2>
<button onClick={this.updateHeader.bind(this)}>Update</button>
</div>
);
}
}
export default StateExample;
- state์ ์ด๊ธฐ๊ฐ์ ์ค์ ํ ๋๋ constructor(์์ฑ์) ํจ์์์
this.state = { }
๋ฅผ ํตํ์ฌ ์ค์ ํ๋ค. - state๋ฅผ ๋ ๋๋งํ ๋๋
{this. state. stateName}
์ ์ฌ์ฉํ๋ค. - state๋ฅผ ์ ๋ฐ์ดํธํ ๋๋ this.setState() ํจ์๋ฅผ ์ฌ์ฉํ๋ค. ES6(ES2015)์์๋ 'auto binding'์ด ๋์ง ์์ผ๋ฏ๋ก, setState ํจ์๋ฅผ ์ฌ์ฉํ๊ฒ ๋ ํจ์๋ฅผ bind ํด์ฃผ์ด์ผ ํ๋ค. (bindํ์ง ์์ผ๋ฉด React Component๊ฐ ๊ฐ์ง๊ณ ์๋ ๋ฉค๋ฒ ํจ์ ๋ฐ ๊ฐ์ฒด์ ์ ๊ทผํ ์ ์๋ค.)
ํน์ฑ | props | state |
---|---|---|
parent ์ปดํฌ๋ํธ์ ์ํด ๊ฐ์ด ๋ณ๊ฒฝ ๋ ์ ์๋๊ฐ? | ์ | ์๋์ค |
์ปดํฌ๋ํธ ๋ด๋ถ์์ ๋ณ๊ฒฝ ๋ ์ ์๋๊ฐ? | ์๋์ค | ์ |