๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

200315

ยท ์•ฝ 5๋ถ„

React#

props ๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ๊ฐ’ ์ „๋‹ฌํ•˜๊ธฐ (๊ณ„์†)#

defaultProps ๋กœ ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •#

Hello.js

import React from 'react';
function Hello({color, name}) {    return <div style={{color}}>์•ˆ๋…•ํ•˜์„ธ์š” {name}</div>;}
Hello.defaultProps = {    name: '์ด๋ฆ„์—†์Œ',};
export default Hello;

์ด๋ ‡๊ฒŒ ์ปดํฌ๋„ŒํŠธ์— props ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜์žˆ๋‹ค.

App.js

import React from 'react';import Hello from './Hello';
function App() {    return (        <>            <Hello name="react" color="red" />            <Hello color="pink" />        </>    );}
export default App;

2020-03-15-200315-image-0

props.children#

์ปดํฌ๋„ŒํŠธ ํƒœ๊ทธ ์‚ฌ์ด์— ๋„ฃ์€ ๊ฐ’์„ ์กฐํšŒํ•˜๊ณ  ์‹ถ์„ ๋•, props.children ์„ ์กฐํšŒํ•˜๋ฉด ๋œ๋‹ค.

props.children ์„ ์‚ฌ์šฉํ•˜๋Š” Wrapper.js ๋ฅผ ๋งŒ๋“ค์–ด ๋ณธ๋‹ค.

Wrapper.js

import React from 'react';
function Wrapper() {    const style = {        border: '2px solid black',        padding: '16px',    };    return <div style={style}></div>;}
export default Wrapper;

App.js

import React from 'react';import Hello from './Hello';import Wrapper from './Wrapper';
function App() {    return (        <Wrapper>            <Hello name="react" color="red" />            <Hello color="pink" />        </Wrapper>    );}
export default App;

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‚ด๋ถ€์˜ ๋‚ด์šฉ์ด ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค. Wrapper.js ์—์„œ props.children ์„๋ Œ๋”๋งํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

Wrapper.js

import React from 'react';
function Wrapper(props) {    // props ๋Œ€์‹  { children } ์œผ๋กœ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น ํ›„    const style = {        border: '2px solid black',        padding: '16px',    };    return (        <div style={style}>            {props.children} {/* ์—ฌ๊ธฐ์„œ children ์œผ๋กœ๋งŒ ๋ฐ›์•„๋„ ๋œ๋‹ค. */}        </div>    );}
export default Wrapper;

2020-03-15-200315-image-1

์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง#

props ๋ฅผ ์ด์šฉํ•˜์—ฌ ํŠน์ • ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฌผ์„ ๋ Œ๋”๋ง ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

App.js

import React from 'react';import Hello from './Hello';import Wrapper from './Wrapper';
function App() {    return (        <Wrapper>            <Hello name="react" color="red" isSpecial={true} />            <Hello color="pink" />        </Wrapper>    );}
export default App;

isSpecial ์ด๋ผ๋Š” props ๋ฅผ ์ „๋‹ฌํ•˜๊ณ , (์ด ๋•Œ true ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ’์ด๋ฏ€๋กœ์ค‘๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์•ผํ•œ๋‹ค.

Hello.js

import React from 'react';
function Hello({color, name, isSpecial}) {    return (        <div style={{color}}>            {isSpecial ? <b>*</b> : null}            ์•ˆ๋…•ํ•˜์„ธ์š” {name}        </div>    );}
Hello.defaultProps = {    name: '์ด๋ฆ„์—†์Œ',};
export default Hello;

์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ๋‹จ์ถ• ํ‰๊ฐ€ ๋…ผ๋ฆฌ ๊ณ„์‚ฐ๋ฒ•์œผ๋กœ isSpecial && <b>*</b> ๋กœ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

๋˜ํ•œ props ๊ฐ’์„ ์„ค์ •ํ•  ๋•Œ ์ด๋ฆ„๋งŒ ์ž‘์„ฑํ•˜๊ณ  ๊ฐ’ ์„ค์ •์„ ์ƒ๋žตํ•  ๊ฒฝ์šฐ ์ด๋ฅผ true ๋กœ์„ค์ •ํ•œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.

App.js

import React from 'react';import Hello from './Hello';import Wrapper from './Wrapper';
function App() {    return (        <Wrapper>            <Hello name="react" color="red" isSpecial />            <Hello color="pink" />        </Wrapper>    );}
export default App;

Hello.js

import React from 'react';
function Hello({color, name, isSpecial}) {    return (        <div style={{color}}>            {isSpecial ? alert(isSpecial) : null}            ์•ˆ๋…•ํ•˜์„ธ์š”            {name}        </div>    );}
Hello.defaultProps = {    name: '์ด๋ฆ„์—†์Œ',};
export default Hello;

isSpecial ์— ๊ฐ’์„ ์„ค์ •ํ•˜์ง€ ์•Š๊ณ  Hello.js ์—์„œ ๋งŒ์•ฝ isSpecial ์ด true ๋ผ๋ฉด isSpecial ์„ alert๋กœ ๋‚ด๋ณด๋‚ด๊ฒŒ ํ–ˆ๋‹ค.

2020-03-15-200315-image-2

true ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.


useState ๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ”๋€Œ๋Š” ๊ฐ’ ๊ด€๋ฆฌํ•˜๊ธฐ#

Counter ์˜ˆ์ œ๋ฅผ react๋กœ ๊ตฌํ˜„ํ•ด ๋ณธ๋‹ค.

App.js

import React from 'react';import Counter from './Counter';
function App() {    return <Counter />;}
export default App;

Counter.js

import React, {useState} from 'react';
function Counter() {    const [number, setNumber] = useState(0);
    const onIncrease = () => {        setNumber(number + 1);    };
    const onDecrease = () => {        setNumber(number - 1);    };
    return (        <div>            <h1>{number}</h1>            <button onClick={onIncrease}>+1</button>            <button onClick={onDecrease}>-1</button>        </div>    );}
export default Counter;

ํ•จ์ˆ˜ํ˜• ์—…๋ฐ์ดํŠธ#

์ด ๋•Œ, setNumber ํ•จ์ˆ˜(Setter ํ•จ์ˆ˜)๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์—…๋ฐ์ดํŠธ ํ•˜๊ณ  ์‹ถ์€ ์ƒˆ๋กœ์šด ๊ฐ’์„ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„ฃ์–ด์ฃผ๊ณ  ์žˆ๋Š”๋ฐ, ๊ทธ ๋Œ€์‹  ๊ธฐ์กด์˜ ๊ฐ’์„ ์–ด๋–ป๊ฒŒ ์—…๋ฐ์ดํŠธ ํ•  ์ง€์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ๋„ ๊ฐ’์„ ์—…๋ฐ์ดํŠธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Counter.js

import React, {useState} from 'react';
function Counter() {    const [number, setNumber] = useState(0);
    const onIncrease = () => {        setNumber((prevNumber) => prevNumber + 1);    };
    const onDecrease = () => {        setNumber((prevNumber) => prevNumber - 1);    };
    return (        <div>            <h1>{number}</h1>            <button onClick={onIncrease}>+1</button>            <button onClick={onDecrease}>-1</button>        </div>    );}
export default Counter;