๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[๊ฐœ๋ฐœ] Practice/React

[React] props ๋Œ€์‹  redux ์‚ฌ์šฉํ•ด๋ณด๊ธฐ_3

by Connecting-the-dots 2022. 3. 29.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ก ์‹ค์Šต ํฌ์ธํŠธ!

  • reducer ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์–ด๋–ค ์‹์œผ๋กœ redux ๋ฅผ ํ™œ์šฉํ•ด์•ผํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ์žฅ๋ฐ”๊ตฌ๋‹ˆ ํŽ˜์ด์ง€์— ์•Œ๋ฆผ์ฐฝ UI ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด๋ณด์•˜๋‹ค.
  • ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ•˜๋‚˜์˜ Component ์—๋งŒ ์‚ฌ์šฉํ•  state ๋ฐ์ดํ„ฐ๋ผ๋ฉด redux ์— ๊ตณ์ด ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์—†์ด useState() ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ์‰ฝ๊ณ  ๊ฐ„๋‹จํ•˜๋‹ค๋Š” ๊ฒƒ๋„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.
  • ์ด๋ฒˆ์—๋Š” reducer ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ฝ”๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ์ž‘์„ฑํ•˜๋Š”์ง€ ๋ฐฐ์šฐ๊ธฐ ์œ„ํ•ด ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๊ธดํ–ˆ์ง€๋งŒ, ๋ณต์Šตํ•˜๋ฉด์„œ useState() ๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐฉ์‹์œผ๋กœ ๋ฐ”๊ฟ€ ์˜ˆ์ •์ด๋‹ค.

๐Ÿ’œ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ํŽ˜์ด์ง€์— ์•Œ๋ฆผ์ฐฝ ๋งŒ๋“ค๊ธฐ

  • ์‹ ๊ทœ๊ฐ€์ž…ํ•˜๋ฉด ์ตœ๋Œ€ 20% ํ• ์ธ ์ฟ ํฐ์„ ์ œ๊ณตํ•œ๋‹ค๋Š” ์•Œ๋ฆผ์ฐฝ์„ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ํŽ˜์ด์ง€ ํ•˜๋‹จ์— ๋„์šฐ๊ณ  ๋‹ซ๊ธฐ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ด๋ณด์•˜๋‹ค.

๐Ÿค ์•Œ๋ฆผ์ฐฝ UI ๋งŒ๋“ค๊ธฐ

let Alert = styled.div`
    background: #eee;
    max-width: none;
    font-weight: bold;
    color: red;
    font-size : 20px;
    padding: 15px 20px;
`
  • ์šฐ์„  styled-components ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ Alert Component ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.
<Alert>
    <span className='me-3'>์‹ ๊ทœ๊ฐ€์ž…์‹œ ์ตœ๋Œ€ 20% ํ• ์ธ ์ฟ ํฐ ์ œ๊ณต!</span>
    <button className='btn btn-outline-danger'>๋‹ซ๊ธฐ</button>
</Alert>
  • ์‹ ๊ทœ๊ฐ€์ž…ํ•˜๋Š” ๊ฒฝ์šฐ ์ตœ๋Œ€ 20% ํ• ์ธ ์ฟ ํฐ์„ ์ œ๊ณตํ•œ๋‹ค๋Š” ๋‚ด์šฉ์„ ์ž‘์„ฑํ•œ ํ›„ Bootstrap ์„ ํ™œ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ๋ ˆ์ด์•„์›ƒ์„ ์žก์•„์ฃผ๊ณ  <Alert></Alert> ๋กœ ๊ฐ์‹ธ์ฃผ์—ˆ๋‹ค.

๐Ÿค reducer ํ•˜๋‚˜ ๋” ๋งŒ๋“ค๊ธฐ

  • useState() ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋˜์ง€๋งŒ reducer ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ํ•„์š”ํ•œ ์ƒํ™ฉ์„ ์—ฐ์ถœํ•˜๊ธฐ ์œ„ํ•ด redux ์— state ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•ด๋ณด์•˜๋‹ค.
  • ์•Œ๋ฆผ์ฐฝ์€ ๋ณด์ด๋„๋ก ํ˜น์€ ์•ˆ ๋ณด์ด๋„๋ก ์„ค์ •ํ•ด์ค„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— true / false ๋ฅผ state ์— ์ €์žฅํ•˜๋ฉด ๋œ๋‹ค.
  • ๊ทธ๋Ÿผ ์–ด๋””์— ์ €์žฅํ•˜๋Š๋ƒ๊ฐ€ ๋ฌธ์ œ๊ฐ€ ๋˜๋Š”๋ฐ ์ผ๋ฐ˜์ ์ธ Component ์—์„œ state ๊ฐ€ ์ถ”๊ฐ€์ ์œผ๋กœ ํ•„์š”ํ•˜๋ฉด useState() ๋ฅผ ํ•˜๋‚˜ ๋” ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๋“ฏ, redux ์—์„œ๋„ reducer ๋ฅผ ํ•˜๋‚˜ ๋” ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
let defaultAlert = true;

function reducer2(state = defaultAlert, action) {
  if (action.type === 'close') {
    state = false;
    return state;
  } else {
    return state;
  }
}
  • reducer ๋ฅผ ํ•˜๋‚˜ ๋” ๋งŒ๋“ค ๋•Œ์—๋Š” state ์ดˆ๊ธฐ๊ฐ’๊ณผ reducer, ์ด๋ ‡๊ฒŒ ํ•œ ์Œ์„ ๋งŒ๋“ค์–ด์ฃผ๋ฉด ๋œ๋‹ค.
    (๊ทธ๋ž˜์„œ ๋‚˜๋Š” defaultAlert ๋ผ๋Š” state ์ดˆ๊ธฐ๊ฐ’๊ณผ, reducer2 ๋ผ๋Š” reducer ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.)
  • ์ฝ”๋“œ๋ฅผ ์ž ๊น ํ•ด์„ํ•˜์ž๋ฉด ๋จผ์ € ์•Œ๋ฆผ์ฐฝ์ด ๋ณด์ด๋Š” ๊ฒƒ์„ true, ์•ˆ ๋ณด์ด๋Š” ๊ฒƒ์„ false ๋กœ ์ •ํ•œ ํ›„์— ์žฅ๋ฐ”๊ตฌ๋‹ˆ ํŽ˜์ด์ง€์— ์ ‘์†ํ–ˆ์„ ๋•Œ๋Š” ์•Œ๋ฆผ์ฐฝ์ด ๋ณด์—ฌ์•ผ ํ•˜๋ฏ€๋กœ ์ดˆ๊ธฐ๊ฐ’์„ true ๋ผ๊ณ  ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค.
  • ๋‹ซ๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ(action.type ์ด 'close'), state ์—๋Š” false ๊ฐ€ ์ €์žฅ๋œ ํ›„์— return ๋˜๊ณ  ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ์—๋Š” ๊ธฐ๋ณธ ์ƒํƒœ๋กœ return ํ•˜๊ฒŒ ํ•ด์ฃผ์—ˆ๋‹ค.

 

let store = createStore(combineReducers({ reducer, reducer2 }));
  • ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ์ด์ „์— ๋งŒ๋“ค์–ด๋‘์—ˆ๋˜ store ์— reducer2 ๋ฅผ ์ถ”๊ฐ€๋กœ ๋“ฑ๋กํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
  • ๋จผ์ €, combineReducers() ๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ redux ๋กœ๋ถ€ํ„ฐ import ํ•ด์ค€๋‹ค.
  • ๊ทธ ๋‹ค์Œ์— createStore() ์•ˆ์— combineReducers() ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ์ด์ „์— ๋งŒ๋“ค๊ณ  ์‚ฌ์šฉํ•œ reducer ์™€ ์ด๋ฒˆ์— ๋งŒ๋“ค์–ด์ค€ reducer2 ๋ฅผ object ํ˜•์‹์œผ๋กœ ๋‹ด์•„์ฃผ๋ฉด ๋œ๋‹ค.

๐Ÿค reducer ์‚ฌ์šฉํ•˜๊ธฐ

// reducer ๊ฐ€ ํ•˜๋‚˜์˜€์„ ๋•Œ

function Store(state) {

    return {
        state : state;
    }

}


// reducer ๊ฐ€ ๋‘ ๊ฐœ์ผ ๋•Œ

function Store(state) {

    return {
        data : state.reducer,
        alert : state.reducer2
    }

}
  • reducer ๊ฐ€ ํ•˜๋‚˜์ผ ๋•Œ์™€ ๋‹ฌ๋ฆฌ 2๊ฐœ ์ด์ƒ์ผ ๋•Œ์—๋Š” state ์•ˆ์— { reducer : [ ], reducer2 : [ ] ... } ์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ, state ๋ฅผ props ๋กœ ์ €์žฅํ•  ๋•Œ state.reducer ๋Š” data ๋กœ, state.reducer2 ๋Š” alert ๋กœ ์ €์žฅํ•ด์ฃผ์—ˆ๋‹ค.

๐Ÿค ์•Œ๋ฆผ์ฐฝ ๋‹ซ๊ธฐ ๋ฒ„ํŠผ ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

{
    props.alert === true ? (
        <Alert>
            <span className='me-3'>์‹ ๊ทœ๊ฐ€์ž…์‹œ ์ตœ๋Œ€ 20% ํ• ์ธ ์ฟ ํฐ ์ œ๊ณต!</span>
            <button className='btn btn-outline-danger' onClick={ ()=>{ props.dispatch({ type : 'close' }) } }>๋‹ซ๊ธฐ</button>
        </Alert>
    ) : null
}
  • ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ „์†ก๋ฐ›์•„์˜จ props.alert ๊ฐ€ true ๋ผ๋ฉด ์•Œ๋ฆผ์ฐฝ์„ ๋ณด์—ฌ์ฃผ๊ณ , false ๋ผ๋ฉด ์•Œ๋ฆผ์ฐฝ์ด ์•ˆ๋ณด์ด๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ๋‹ซ๊ธฐ ๋ฒ„ํŠผ์— onClick ์†์„ฑ์„ ๋„ฃ์–ด, ๋‹ซ๊ธฐ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ action.type ์ด 'close' ๊ฐ€ ๋˜์–ด(์ด ๊ณผ์ •์—์„œ state ๊ฐ€ false ๋กœ ๋ฐ”๋€๋‹ค.) ์•Œ๋ฆผ์ฐฝ์ด ์•ˆ ๋ณด์ด๋„๋ก ํ•ด์ฃผ์—ˆ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•