[React] props

๊ฐ•์€๋น„ยท2022๋…„ 1์›” 11์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
6/36
post-custom-banner

react ์Šคํ„ฐ๋””์—์„œ ๋ฆฌ์•กํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ธฐ์ˆ ์ด๋ผ๋Š” ์ฑ…์„ ์„ ์ •ํ–ˆ๊ณ  ์ด ์ฑ…์„ ์ฝ๊ณ  ๋ฐฐ์šด ๊ฒƒ์„ ๋ฐ”ํƒ•์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ๋‹ค.


๐Ÿ“Œ props

properties์˜ ์ค„์ž„๋ง

  • ์ปดํฌ๋„ŒํŠธ์˜ ์†์„ฑ์„ ์„ค์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์š”์†Œ์ด๋‹ค.
  • props ๊ฐ’์€ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“™ JSX ๋‚ด๋ถ€์—์„œ props ๋ Œ๋”๋ง

  • props ๊ฐ’์€ ์ปดํฌ๋„ŒํŠธ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์™€์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • props๋ฅผ JSX ๋‚ด๋ถ€์—์„œ ๋ Œ๋”๋งํ•  ๋•Œ {} ๊ธฐํ˜ธ๋กœ ๊ฐ์‹ธ ์ฃผ๋ฉด ๋œ๋‹ค.
const MyComponent = (props) => {
    return <div>์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ์ด๋ฆ„์€ {props.name}์ž…๋‹ˆ๋‹ค.</div>;
}
export default MyComponent;
import MyComponent from "./MyComponent";

// MyComponent์˜ ๋ถ€๋ชจ ์š”์†Œ์—์„œ props ๊ฐ’ ์„ค์ •
const App = () => {
    return <MyComponent name="React"/>;
}

export default App;

๐Ÿ“™ defaultProps

  • props์˜ ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •ํ•  ๋•Œ ์‚ฌ์šฉ
const MyComponent = (props) => {
    return <div>์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ์ด๋ฆ„์€ {props.name}์ž…๋‹ˆ๋‹ค.</div>;
}

MyComponent.defaultProps = {
    name: "๊ธฐ๋ณธ ์ด๋ฆ„"
};

export default MyComponent;

๐Ÿ“™ props.children

  • ์ปดํฌ๋„ŒํŠธ ํƒœ๊ทธ ์‚ฌ์ด์˜ ๋‚ด์šฉ์„ ์กฐํšŒํ•  ๋•Œ ์‚ฌ์šฉ
const MyComponent = (props) => {
    return (
        <div>
            <p>์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ์ด๋ฆ„์€ {props.name}์ž…๋‹ˆ๋‹ค.</p>
            <p>children ๊ฐ’์€ {props.children>์ž…๋‚˜๋‹ค.</p>
        </div>;
}

MyComponent.defaultProps = {
    name: "๊ธฐ๋ณธ ์ด๋ฆ„"
};

export default MyComponent;
import MyComponent from "./MyComponent";

// MyComponent์˜ ๋ถ€๋ชจ ์š”์†Œ์—์„œ props ๊ฐ’ ์„ค์ •
const App = () => {
    return <MyComponent>๋ฆฌ์•กํŠธ</MyComponent>;
}

export default App;

๐Ÿ“™ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น ๋ฌธ๋ฒ•์„ ํ†ตํ•ด props ๋‚ด๋ถ€ ๊ฐ’ ์ถ”์ถœ

destructuring assignment

const MyComponent = (props) => {
    // ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น ๋ฌธ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ props ๋‚ด๋ถ€ ๊ฐ’ ์ถ”์ถœ
    const {name, children} = props;
    return (
        <div>
            <p>์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ์ด๋ฆ„์€ {name}์ž…๋‹ˆ๋‹ค.</p>
            <p>children ๊ฐ’์€ {children>์ž…๋‚˜๋‹ค.</p>
        </div>;
}

MyComponent.defaultProps = {
    name: "๊ธฐ๋ณธ ์ด๋ฆ„"
};

export default MyComponent;
// ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๊ฐ์ฒด์ผ ๋•Œ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น ๋ฌธ๋ฒ• ์ ์šฉ ๊ฐ€๋Šฅ
const MyComponent = ({name, children}) => {
    return (
        <div>
            <p>์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ์ด๋ฆ„์€ {name}์ž…๋‹ˆ๋‹ค.</p>
            <p>children ๊ฐ’์€ {children>์ž…๋‚˜๋‹ค.</p>
        </div>;
}

MyComponent.defaultProps = {
    name: "๊ธฐ๋ณธ ์ด๋ฆ„"
};

export default MyComponent;

๐Ÿ“™ propTypes๋ฅผ ํ†ตํ•œ props ๊ฒ€์ฆ

  • ์ปดํฌ๋„ŒํŠธ์˜ ํ•„์ˆ˜ props๋ฅผ ์ง€์ •ํ•˜๊ฑฐ๋‚˜ props์˜ ํƒ€์ž…์„ ์ง€์ •ํ•  ๋•Œ ์‚ฌ์šฉ
import PropTypes form "prop-types";

...

Food.propTypes = {
    name: PropTypes.string.isRequired,
    picture: PropTypes.string.isRequired,
    rating: PropTypes.number.isRequired,
};

PropsTypes ์ข…๋ฅ˜

  • array: ๋ฐฐ์—ด
  • arrayOf(๋‹ค๋ฅธ PropType): ํŠน์ • PropType์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด
    • arrayOf(PropType.number): ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด
  • bool: true ํ˜น์€ false
  • func: ํ•จ์ˆ˜
  • number: ์ˆซ์ž
  • object: ๊ฐ์ฒด
  • string: ๋ฌธ์ž์—ด
  • symbol: ES6 Symbol
  • node: ๋ Œ๋”๋งํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒƒ (์ˆซ์ž, ๋ฌธ์ž์—ด, ํ˜น์€ JSX ์ฝ”๋“œ, children๋„ node Prototype์ด๋‹ค.)
  • instanceOf(class): ํŠน์ • ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค
  • oneOf([]): ์ฃผ์–ด์ง„ ๋ฐฐ์—ด ์š”์†Œ ๊ฐ’ ์ค‘ ํ•˜๋‚˜
  • oneOfType([]): ์ฃผ์–ด์ง„ ๋ฐฐ์—ด ์•ˆ์˜ ์ข…๋ฅ˜ ์ค‘ ํ•˜๋‚˜
  • objectOf();: ๊ฐ์ฒด์˜ ๋ชจ๋“  ํ‚ค ๊ฐ’์ด ์ธ์ž๋กœ ์ฃผ์–ด์ง„ PropType์ธ ๊ฐ์ฒด
  • shape({ name: PropTypes.string, num: PropTypes.number }): ์ฃผ์–ด์ง„ ์Šคํ‚ค๋งˆ๋ฅผ ๊ฐ€์ง„ ๊ฐ์ฒด
  • any: ์•„๋ฌด ์ข…๋ฅ˜

๐Ÿ“™ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ props ์‚ฌ์šฉํ•˜๊ธฐ

  • ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ props๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ render ๋ฉ”์„œ๋“œ์—์„œ this.props๋ฅผ ์กฐํšŒํ•˜๋ฉด ๋œ๋‹ค.
  • ์•ž์„  ๋˜‘๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ defaultProps์™€ propTypes๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. (ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋„ ์„ค์ • ๊ฐ€๋Šฅ)
static defalutProps = {
    name: "๊ธฐ๋ณธ ์ด๋ฆ„"
};

static propTypes = {
    name: PropTypes.string,
    favoriteNumber: PropTypes.number.isRequired
};
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€