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

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

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

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

  • ๊ธฐ์กด์— ์ž‘์„ฑํ–ˆ๋˜ state ๋ฅผ props ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ•จ์ˆ˜ ๋ฐ export default connect()() ๋Š” ์ƒ๋Œ€์ ์œผ๋กœ ์˜ˆ์ „์— ์“ฐ์ด๋˜ ๋ฐฉ์‹์ด์ง€๋งŒ ํ˜„์—…์—์„œ ์ฝ”๋“œ๋ฅผ ์ฝ์–ด๋‚ผ ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ ์ด์ „ ์ˆ˜์—…์„ ํ†ตํ•ด ๋ฐฐ์›Œ๋ณด์•˜๋‹ค.
  • ์ด๋ฒˆ์—๋Š” useSelector() ์™€ useDispatch() ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ›จ์”ฌ ๋” ์‰ฝ๊ณ  ๊ฐ„๋žตํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›Œ๋ณด์•˜๋‹ค.
  • ์ˆ˜์—…์— ๋ฐฐ์šด ๊ฒƒ๋“ค์„ ํ™œ์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ํŒŒ์ผ์—๋„ ์ ์šฉํ•ด๋ณด์•˜๋‹ค.

๐Ÿ’œ ์ „์ฒด ์ฝ”๋“œ ๋ฏธ๋ฆฌ๋ณด๊ธฐ

// ์ „์ฒด ์ฝ”๋“œ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
// ์ „์ฒด ์ฝ”๋“œ์˜ ๋‚ด์šฉ์ด ๋„ˆ๋ฌด ๊ธธ์–ด ๋ถˆํ•„์š”ํ•œ ๋ถ€๋ถ„์€ ์ œ์™ธํ–ˆ๋‹ค.

import { useDispatch, useSelector } from 'react-redux';

function Cart(props) {

    let state = useSelector((state)=>state);
    let dispatch = useDispatch();

    return(
        <CartContainer>
            <Table responsive>
                <thead>
                <tr>
                    <th>#</th>
                    <th>์ƒํ’ˆ๋ช…</th>
                    <th>์ˆ˜๋Ÿ‰</th>
                </tr>
                </thead>
                <tbody>
                    {
                        state.reducer.map((item, idx)=>{
                            return (
                                <tr key={idx}>
                                    <td>{ idx + 1 }</td>
                                    <td>{item.name}</td>
                                    <td>
                                        <ChangeNum onClick={ ()=>{ dispatch({ type : 'plus' , payload : idx }) } }>+</ChangeNum>
                                            {item.quan}
                                        <ChangeNum onClick={ ()=>{ dispatch({ type : 'minus', payload : id : idx }) } }>-</ChangeNum>
                                    </td>
                                </tr>
                            )
                        })
                    }
                </tbody>
            </Table>
            {
                state.reducer2 === true ? (
                    <Alert>
                        <span className='me-3'>์‹ ๊ทœ๊ฐ€์ž…์‹œ ์ตœ๋Œ€ 20% ํ• ์ธ ์ฟ ํฐ ์ œ๊ณต!</span>
                        <button className='btn btn-outline-danger' onClick={ ()=>{ dispatch({ type : 'close' }) } }>๋‹ซ๊ธฐ</button>
                    </Alert>
                ) : null
            }
        </CartContainer>
    )
}

export default Cart;

๐Ÿ’œ useSelector

  • redux store ์— ์ €์žฅ๋˜์–ด์žˆ๋˜ state ๋ฅผ ๊บผ๋‚ด์–ด ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” state ๋ฅผ props ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  export default connect()() ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์ด์ „ ์ˆ˜์—…๊นŒ์ง€ ๊ณต๋ถ€ํ–ˆ๋‹ค.
  • ์‚ฌ์‹ค ์ด๊ฒƒ์€ ์˜›๋‚  ๋ฐฉ์‹์ด๋ฉฐ ์š”์ฆ˜์—๋Š” useSelector ๋ผ๋Š” Hook ์„ ์‚ฌ์šฉํ•ด์„œ ๋” ์‰ฝ๊ฒŒ state ๋ฅผ ๊บผ๋‚ด์–ด ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

๐Ÿค useSelector ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์•Œ์•„๋ณด๊ธฐ

function Store(state) {

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

}

export default connect(Store)(Cart);
  • ์ด์ „์—๋Š” ์œ„์™€ ๊ฐ™์ด state ๋ฅผ props ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ•จ์ˆ˜(์œ„ ์ฝ”๋“œ์˜ Store() ํ•จ์ˆ˜)์™€ export default connect()() ๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ์ด์ œ ํ•จ์ˆ˜๋Š” ์ง€์›Œ์ฃผ๋ฉด ๋œ๋‹ค.
export default Cart;
  • ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์ค„์˜ export default ์ฝ”๋“œ๋Š” connect() ๋Œ€์‹  ๊ทธ๋ƒฅ Component ์ด๋ฆ„์ธ Cart ๋กœ ๋ฐ”๊พธ์–ด์ค€๋‹ค.

 

 

import { useDispatch, useSelector } from 'react-redux';

function Cart(props) {

    let state = useSelector((state)=>state);
    let dispatch = useDispatch();

    return(
        <CartContainer>
		  // ์ƒ๋žต
        </CartContainer>
    )
}

export default Cart;
  • ์ด์ œ useSelector() ๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ import ํ•œ ๋‹ค์Œ, useSelector() ์•ˆ์— ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์ด๋ฅผ state ๋ผ๋Š” ๋ณ€์ˆ˜์— ์ €์žฅํ•ด์ค€๋‹ค.
  • ์ด ๋•Œ, ์ฝœ๋ฐฑํ•จ์ˆ˜ ์•ˆ์—๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ํ•˜๋‚˜๋งŒ ์ž…๋ ฅ ๊ฐ€๋Šฅํ•œ๋ฐ ์ด๋Š” ์ž๋™์œผ๋กœ reducer ๋“ค์„ ํ•ฉ์นœ store ๊ฐ€ ๋œ๋‹ค.
    ((state) => state ๋ผ๊ณ  ์ž‘์„ฑํ•˜๋ฉด state ๋“ค์„ state ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„ state ๋กœ ์ €์žฅํ•œ๋‹ค๋Š” ์˜๋ฏธ๊ฐ€ ๋˜๋ฉฐ ๋” ์„ธ๋ถ€์ ์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.) 
  • ์ด์ œ ๊ธฐ์กด์˜ ์ฝ”๋“œ๋“ค ์ค‘ ์ˆ˜์ •์ด ํ•„์š”ํ•œ ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 
    (์˜ˆ๋ฅผ ๋“ค์–ด, props.data.map~ ๋ถ€๋ถ„์€ state.reducer.map~ ์ด๋Ÿฐ ์‹์œผ๋กœ...)

๐Ÿ’œ useDispatch

๐Ÿค useDispatch ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์•Œ์•„๋ณด๊ธฐ

import { useDispatch, useSelector } from 'react-redux';

function Cart(props) {

    let state = useSelector((state)=>state);
    let dispatch = useDispatch();

    return(
        <CartContainer>
		  // ์ƒ๋žต
        </CartContainer>
    )
}

export default Cart;
  • useDispatch() ๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ import ํ•œ ๋‹ค์Œ, useDispatch() ๋ฅผ dispatch ๋ผ๋Š” ๋ณ€์ˆ˜์— ์ €์žฅํ•ด์ค€๋‹ค.
  • ๊ทธ๋Ÿฌ๋ฉด ๊ธฐ์กด์— props.dispatch() ๋กœ state ์ˆ˜์ • ์š”์ฒญ์„ ํ•˜๋˜ ๊ฒƒ์„ dispatch() ๋งŒ์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฝ”๋“œ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.
  • ์ด์ œ ๊ธฐ์กด์˜ ์ฝ”๋“œ๋“ค ์ค‘ ์ˆ˜์ •์ด ํ•„์š”ํ•œ ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
    (์˜ˆ๋ฅผ ๋“ค์–ด, props.dispatch() ๋ถ€๋ถ„์„ dispatch() ์ด๋Ÿฐ ์‹์œผ๋กœ...)

 

728x90
๋ฐ˜์‘ํ˜•