- Props
- State
- ์์ฝ
properties์ ์ค์๋ง๋ก, ์ปดํฌ๋ํธ ์์ฑ์ ์ค์ ํ ๋ ์ฌ์ฉํ๋ ์์์ด๋ค.
props์ ๊ฐ์ ํด๋น ์ปดํฌ๋ํธ๋ฅผ ๋ถ๋ฌ์ ์ฌ์ฉํ๋ ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ์ค์ ํ ์ ์๋ค.
props ๊ฐ์ ์ปดํฌ๋ํธ ํจ์์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ ์์ ์ฌ์ฉํ ์ ์๋ค.
Examples.js (ํด๋์คํ ์ปดํฌ๋ํธ ์ด์ฉ)
import React, { Component } from 'react';
import PropTypes from 'prop-types';
class Examples extends Component {
// ํด๋์ค ๋ด๋ถ์์ defaultProps์ propTypes ์ง์ ํ๋ ๋ฐฉ๋ฒ.
static defaultProps = {
name: '๊ธฐ๋ณธ ์ด๋ฆ',
};
static propTypes = {
name: PropTypes.string,
favoriteNumber: PropTypes.number.isRequired,
};
render() {
const { name, children, favoriteNumber } = this.props;
return (
<div>
์๋
ํ์ธ์, ์ ์ด๋ฆ์ {name} ์
๋๋ค. <br />
children ๊ฐ์ {children} ์
๋๋ค. <br />
์ ๊ฐ ์ข์ํ๋ ์ซ์๋ {favoriteNumber} ์
๋๋ค.
</div>
);
}
}
// ํด๋์ค ๋ฐ์์ defaultProps ์ง์ ํ๋ ๋ฐฉ๋ฒ.
//
// Examples.defaultProps = {
// name: '๊ธฐ๋ณธ ์ด๋ฆ',
// };
// Examples.propTypes = {
// name: PropTypes.string,
// favoriteNumber: PropTypes.number.isRequired,
// };
export default Examples;
Example.js (ํจ์ ์ปดํฌ๋ํธ)
import React from 'react';
import PropTypes from 'prop-types';
const Example = ({ name, children, favoriteNumber }) => {
return (
<div>
์๋
ํ์ธ์, ์ ์ด๋ฆ์ {name} ์
๋๋ค. <br />
children ๊ฐ์ {children} ์
๋๋ค. <br />
์ ๊ฐ ์ข์ํ๋ ์ซ์๋ {favoriteNumber} ์
๋๋ค.
</div>
);
};
Example.defaultProps = {
name: '๊ธฐ๋ณธ ์ด๋ฆ',
};
Example.propTypes = {
name: PropTypes.string,
favoriteNumber: PropTypes.number.isRequired,
};
export default Example;
App.js
import React from 'react';
import Example from './Example';
const App = () => {
return
<>
<MyComponent name="React" favoriteNumber={3}>
๋ฆฌ์กํธ
</MyComponent>
<hr />
<MyComponents name="react" favoriteNumber={7}>
๋ฆฌ์ํธ
</MyComponents>
</>
};
export default App;
๋น๊ตฌ์กฐํ ํ ๋น
์๋ฅผ ๋ค์ด, ๋ค์๊ณผ ๊ฐ์ด props ๊ฐ์ ์กฐํํ ๋๋ง๋ค, props.name, props.children์ฒ๋ผ props. ์ด๋ผ๋ ํค์๋๋ฅผ ๋ถ์ฌ์ฃผ๊ณ ์๋ค.
import React from 'react';
const Example = props => {
return (
<div>
์๋
ํ์ธ์ {props.name} ์
๋๋ค. <br/>
children ๊ฐ์ {props.children} ์
๋๋ค.
</div>
)
}
export default Example;
์ด๋ฅผ ๋ ํธํ๊ฒ ํ๊ธฐ์ํด์ ES6์ ๋น๊ตฌ์กฐํ ํ ๋น ๋ฌธ๋ฒ์ ์ฌ์ฉํด์ ๋ด๋ถ ๊ฐ์ ๋ฐ๋ก ์ถ์ถํ ์ ์๋ค.
import React from 'react';
const Example = props => {
const {name, children} = props;
return (
<div>
์๋
ํ์ธ์ {name} ์
๋๋ค. <br/>
children ๊ฐ์ {children} ์
๋๋ค.
</div>
)
}
export default Example;
์ด๋ฅผ ๋์ฑ ๊ฐ์ํ ํ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋ํ๋ผ ์ ์๋ค.
import React from 'react';
const Example = ({name, children}) => {
return (
<div>
์๋
ํ์ธ์ {name} ์
๋๋ค. <br/>
children ๊ฐ์ {children} ์
๋๋ค.
</div>
)
}
export default Example;
์ด์ฒ๋ผ, ๊ฐ์ฒด์์ ๊ฐ์ ์ถ์ถํ๋ ๋ฌธ๋ฒ์ ๋น๊ตฌ์กฐํ ํ ๋น (destructuring assignment) ๋ผ๊ณ ํ๋ค.
ํจ์์ ํ๋ผ๋ฏธํฐ ๋ถ๋ถ์์๋ ์ฌ์ฉ๊ฐ๋ฅํ๋ฉฐ ํจ์์ ํ๋ผ๋ฏธํฐ๊ฐ ๊ฐ์ฒด์ผ ๊ฒฝ์ฐ, ๊ทธ ๊ฐ์ ๋ฐ๋ก ๋น๊ตฌ์กฐํ ํด์ ์ฌ์ฉํ ์ ์๋ค.
Props ๊ฒ์ฆ
์ปดํฌ๋ํธ์ ํ์ props๋ฅผ ์ง์ ํ๊ฑฐ๋, props ํ์ ์ ์ง์ ํ ๋๋ propTypes๋ฅผ ์ฌ์ฉํ๋ค.
์ฐ์ ๋ชจ๋ prop-types๋ฅผ ์ค์นํด์ฃผ์.
$ yarn add prop-types
๋ค์๊ณผ ๊ฐ์ด ์ค์ ํด์ฃผ๋ฉด, name์ ๊ฐ์ ๋ฌด์กฐ๊ฑด ๋ฌธ์ํ ํํ๋ก ์ ๋ฌ๋์ผ ํ๋ค.
import React from 'react';
import PropTypes from 'prop-types';
const Example = ({name, children}) => {
return (...); // ์๋ต
}
Example.propTypes = {
name: PropTypes.string
};
export default Example;
propTypes๋ฅผ ์ง์ ํ์ง ์์์ ๋ ๊ฒฝ๊ณ ๋ฉ์ธ์ง๋ฅผ ์ฃผ๋ ค๋ฉด propTypes ๋ค์ isRequired๋ฅผ ๋ถ์ฌ์ฃผ๋ฉด ๋๋ค.
import React from 'react';
import PropTypes from 'prop-types';
const Example = ({name, children, favNumber}) => {
return
<div>
(...) // ์๋ต
์ ๊ฐ ์ข์ํ๋ ์ซ์๋ {favNumber} ์
๋๋ค.
</div>
}
Example.propTypes = {
name: PropTypes.string
favNumber: PropTypes.number.isRequired
};
export default Example;
name์ ๋ฌธ์์ด ์ด์ธ์ ํ์ ์ด ์ ๋ฌ๋๊ฑฐ๋ favNumber์ propType๋ฅผ ์ง์ ํ์ง ์์ผ๋ฉด ์ฝ์์์ ์๋ฌ๋ฅผ ๋ฐ์์ํจ๋ค.
ํด๋์คํ ์ปดํฌ๋ํธ์์์ ํ์ฉ์ ์๋ตํ๋๋ก ํ๊ฒ ๋ค.
ํด๋์คํ ์ปดํฌ๋ํธ๊ฐ ๊ฐ์ง๋ state
import React, { Component } from 'react';
class Counter extends Component {
constructor(props) {
super(props);
this.state = {
number: 0,
fixedNumber: 0,
};
}
๋ค์๊ณผ ๊ฐ์ด constructor์ ์ฌ์ฉํด์ state์ ์ด๊ธฐ๊ฐ์ ์ง์ ํด์ค๋ค.
render() {
const { number, fixedNumber } = this.state; // state ์กฐํ ์ this.state๋ก ์กฐํํ๋ค.
return (
<div>
<h1>{number}</h1>
<h2>๋ฐ๋์ง ์๋ ๊ฐ : {fixedNumber}</h2>
<button
// onClick์ ํตํด ๋ฒํผ์ด ํด๋ฆญ๋์์ ๋ ํธ์ถํ ํจ์๋ฅผ ์ง์ ํ๋ค.
// onClick์ ๋ฒํผ ์์ props๋ก ๋ค์ด๊ฐ๋ค. ์ด๊ฒ์ ๋ฒํผ์ด ํด๋ฆญ๋ ๋ ํธ์ถ์ํฌ ํจ์๋ฅผ ์ค์ ํด์ค๋ค. => ์ด๊ฒ์ ์ด๋ฒคํธ ์ค์ ์ด๋ผ๊ณ ํ๋ค.
onClick={() => {
// ์ด๋ฒคํธ ์ค์ ์ ํ์ดํ ํจ์๋ฌธ๋ฒ์ผ๋ก ๋ฃ์ด์ค์ผ ํ๋ค.
// this.setState๋ฅผ ์ฌ์ฉํด์ state์ ์๋ก์ด ๊ฐ์ ๋ฃ์ ์ ์๋ค. (state๊ฐ์ ๋ฐ๊ฟ ์ ์๊ฒ ํด์ค๋ค, state ๊ฐ ์
๋ฐ์ดํธ)
this.setState({ number: number + 1 });
this.setState({ number: this.state.number + 1 });
}}>
+ 1
</button>
</div>
);
}
render ํจ์์์ ํ์ฌ state๋ฅผ ์กฐํํ๊ณ , ์ด๋ฒคํธ๋ฅผ ์ค์ ํด์ฃผ์.
๋ค์๊ณผ ๊ฐ์ด state๋ฅผ constructor์์ ๊บผ๋ด์ state ์ด๊ธฐ๊ฐ ์ค์ ํ ์ ์๋ค.
import React, {Component} from 'react';
class Counter extends Component{
state ={
number: 0,
fixedNumber: 0
};
render() {
const {number, fixedNumber} = this.state;
return ~~;
}
}
export default Counter;
this.setState ์ฌ์ฉ ์, ๊ฐ์ฒด ๋์ ํจ์๋ฅผ ์ธ์๋ก ๋ฃ์ด์ฃผ๋ ๋ฐฉ์
this.setState ((prevState, props) => {
return {
// ์
๋ฐ์ดํธ ๋ด์ฉ
}
});
ํ์ดํ ํจ์์์ ๊ฐ์ ๋ฐ๋ก ๋ฐํํ๊ณ ์ถ์ ๋๋ { }์ ์๋ตํ ์ ์๋ค.
this.setState(prevState => ({
number: prevState.number + 1
}));
this.setState ํ์ ํน์ ์์ ์คํ
this.setState(
{
number: number + 1
},
() => {
console.log('setState ํธ์ถ๋์๋ค.');
}
)
ํจ์ํ ์ปดํฌ๋ํธ์์ state ์ฌ์ฉํ๊ธฐ
๋ฐฐ์ด ๋น๊ตฌ์กฐํ ํ ๋น
const arr = [1, 2];
const first = arr[0];
const second = arr[1];
๋ค์๊ณผ ๊ฐ์ด ๋ฐฐ์ด arr์ ์๋ ๊ฐ์ first, second์ ๋ด์์ค๋ค.
์ด๋ฅผ ์ฝ๊ฒ ์ถ์ถํ๊ธฐ ์ํด ๋ฐฐ์ด ๋น๊ตฌ์กฐํ ํ ๋น์ ์ฌ์ฉํด๋ณด์.
const arr = [1, 2];
const [first, second] = arr;
๊น-๋
useState ์ฌ์ฉ
import React, {useState} from 'react';
const Example = () => {
const[msg, setMsg] = useState('');
const onClickEnter = () => setMsg('hi !');
const onClickLeave = () => setMsg('bye ~');
return (
<div>
<button onClick={onClickEnter}>enter</button>
<button onClick={onClickLeave}>leave</button>
<h2>{msg}</h2>
</div>
)
}
export default Example;
State ์ฌ์ฉ ์ ์ฃผ์์ฌํญ
state ๊ฐ์ ๋ณ๊ฒฝํ ๋๋ setState or useState๋ฅผ ํตํด ์ ๋ฌ๋ฐ์ setter ํจ์๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
๋ฐฐ์ด ํน์ ๊ฐ์ฒด๋ฅผ ์ ๋ฐ์ดํธ ํ ๋
// ๊ฐ์ฒด
const obj = {a: 1, b: 2, c: 3};
const afterObj = {...obj, b: 2}; // ์ฌ๋ณธ ์์ฑ ํ b๊ฐ๋ง ๋ฎ์ด์ด๋ค.
// ... ์ spread ์ฐ์ฐ์์ด๋ฉฐ ๊ฐ์ฒด์ ๋ํ ์ฌ๋ณธ์ ๋ง๋ค ๋ ์ฌ์ฉํ๋ค.
// ๋ฐฐ์ด
const arr = [
{id: 1, value: true},
{id: 2, value: false},
{id: 3, value: false},
];
let nextArr = arr.concat({id:4}); // ์ ํญ๋ชฉ ์ถ๊ฐ
nextArr.filter(item => item.id != = 2); // id๊ฐ 2์ธ ํญ๋ชฉ ์ ๊ฑฐ
nextArr.map(item => (item.id === 1 ? {...item, value: false} : item));
// id๊ฐ 1์ธ ํญ๋ชฉ์ value๋ฅผ false๋ก ์ค์ .