๐ŸคRedux - Part 2. React + Redux (5)

1. Home.js ๋‚ด ul ๋ Œ๋”๋ง ์ˆ˜์ •

return (
  <>
    ..... ์ค‘๋žต .....
    <ul>
      {props.toDos.map(toDo => (
        <ToDo {...toDo} key={toDo.id} />
        // {text: "asd", id: 1610861319410}
      ))}
    </ul>
  </>
)

props ๋ฅผ ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ props ์˜ ์ด๋ฆ„์„ ๋ช…์‹œํ•˜์ง€ ์•Š๊ณ ๋„,

๊ฐ์ฒด ์•ˆ์— spread operator ๋งŒ์œผ๋กœ๋„ ๋‚ด๋ฆด ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

2. li ๋ฅผ ๋ฟŒ๋ ค์ค„ ToDo ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑํ•˜๊ธฐ

const ToDo = props => {
  return (
    <li>
      {props.text} <button>DEL</button>
    </li>
  )
}

์ปดํฌ๋„ŒํŠธ ์ธ์ž์— ({ text }) ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ์ง€๋งŒ์€ ๋‚˜๋Š” console ๋กœ๊ทธ๋กœ ๋‹ค์‹œ ์ž˜ ํ™•์ธํ•ด ๋ณด๊ณ  ์‹ถ์–ด์„œ ์ผ๋ถ€๋Ÿฌ props ๋กœ ์ด์–ด ๋ฐ›๊ฒŒ ํ–ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด Home.js ์— ์žˆ๋Š” handleSubmit ํ•จ์ˆ˜๊ฐ€ mapDispatchToProps ๋ฅผ ํ†ตํ•ด ์ „๋‹ฌํ•œ props.addToDo(text) ๋ฅผ ์‹คํ–‰ํ•ด์„œ,

reducer ์—์„œ toDos ์˜ state ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค.

์ฆ‰, ํด๋ผ์ด์–ธํŠธ์—์„œ ์ถ”๊ฐ€ ๋˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๋Œ€๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

3. deleteToDo ๊ตฌํ˜„ํ•˜๊ธฐ

์ž‘์„ฑํ•œ todo ๋ฅผ ์‚ญ์ œ ํ•˜๋Š” ๊ฒƒ์„ ํ•˜๋ ค๋ฉด reducer ์—์„œ ์ƒํƒœ ๋ณ€๊ฒฝ์„ ์ผ์œผํ‚ค๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

์ƒํƒœ ๋ณ€๊ฒฝ์„ ์ผ์œผํ‚ค๊ฒŒ ํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ”๋กœ

store.dispatch(actionHandler()) ์ด๋‹ค.

ToDo Component ๋Š” ์˜ค๋กœ์ง€ dispatch ๋งŒ ์‹ ๊ฒฝ์“ด๋‹ค.

reducer ์— deleteToDo ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” action handler ํ•จ์ˆ˜๋ฅผ ๋ณด๋‚ธ ๋’ค reducer ์—์„œ ์‹ค์ œ ์ƒํƒœ๋ฅผ ์ง€์šฐ๊ฒŒ ๋ณ€๊ฒฝํ•˜๋„๋ก ๋งŒ๋“œ๋Š” ๊ฑฐ์ง€!

๊ทธ๋Ÿผ ์–ด๋–ค ๋ฐฉ๋ฒ•์œผ๋กœ dispatch ๋™์ž‘์— ์ ‘๊ทผ ํ•˜๋Š”๊ฐ€?

mapStateToProps, mapDispatchToProps ์ค‘์—์„œ, mapDispatchToProps ๋ฅผ ์“ธ ๊ฒƒ์ด๋‹ค. ๋ฐ”๋กœ ์ด ์ปดํฌ๋„ŒํŠธ์—!

์™œ๋ƒํ•˜๋ฉด ToDo ์—์„œ reducer ์—๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ (์ƒํƒœ๋ฅผ ์ง€์šฐ๋„๋ก) ๋ณด๋‚ด๊ธฐ ์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

ToDo Component ์—์„œ๋Š” state ์— ๋Œ€ํ•ด์„œ๋Š” ์‹ ๊ฒฝ์“ธ ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ mapDispatchToProps ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

4. mapDispatchToProps ๊ณผ ownProps

const mapDispatchToProps = (dispatch, ownProps) => {
  console.log(ownProps)
}

์ด๋ฒˆ์—๋Š” ownProps ๋„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ผ ํŒŒ๋ผ๋ฏธํ„ฐ์— dispatch, ownProps ๋„ ๊ฐ™์ด ๋„ฃ์—ˆ๋‹ค.

console.log ๋กœ ownProps ๊ฐ€ ์–ด๋–ค ๊ฒƒ์„ ๋ฐ›๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์•˜๋‹ค.

{text: "hello", id: 1610862082393}

์ด์ œ mapDispatchToProps ๋ฅผ ํ†ตํ•ด ํ˜„์žฌ ToDo Componenet ์— props ๋กœ mapDispatchToProps ๊ฐ€

๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ export ๋ถ€๋ถ„์„ ๊ณ ์ณ ๋†“์ž.

export default connect(null, mapDispatchToProps)(ToDo)

mapStateToProps ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ๋Š” null ๋กœ ๋‘๋ฉด ๋œ๋‹ค.

5. mapDispatchToProps ๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ๊ฐ์ฒด

const mapDispatchToProps = (dispatch, ownProps) => {
  //   console.log(ownProps);
  return {
    onBtnClick: () => dispatch(actionCreators.deleteToDo(ownProps.id)),
  }
}

onBtnClick ์€ ์ด์ œ ํ˜„์žฌ ์ปดํฌ๋„ŒํŠธ์—์„œ props ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ onBtnClick ์ด ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์€ ๋ฐ”๋กœ dispatch ์ด๋‹ค.

์•ˆ์— ์•ก์…˜ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜์ธ deleteToDo ๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ownProps ์˜ id ๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

6. ToDo ์ปดํฌ๋„ŒํŠธ ์ˆ˜์ •, button ์— onClick ์ ์šฉํ•˜๊ธฐ

const ToDo = props => {
  console.log(props)
  // ? {text: "hi", id: 1610862445794, onBtnClick: ฦ’}
  return (
    <li>
      {props.text} <button onClick={props.onBtnClick}>DEL</button>
    </li>
  )
}

์œ„์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋“ฏ ์ด์ œ props ์— onBtnClick ์†์„ฑ์ด ๋“ค์–ด์™€ ์žˆ๋‹ค.

์ด์ œ ์ด ํ•จ์ˆ˜๋ฅผ ๋ฒ„ํŠผ์˜ onClick ์— ์—ฐ๊ฒฐํ•˜์ž.

react redux 3


Written by@[DotoriMook]
ํ”„๋ก ํŠธ์—”๋“œ ์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž ๋„ํ† ๋ฆฌ๋ฌต์˜ ๊ธฐ์ˆ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค.

GitHubMediumTwitterFacebook