Skip to content

Redux and Architecture

Heesun Kim edited this page Nov 13, 2019 · 3 revisions

redux

reduxλž€?

  • reduxλŠ” flux의 κ΅¬ν˜„μ²΄μ΄λ‹€. [flux]

redux의 3가지 원칙

  1. Single Source of Truth (단 ν•˜λ‚˜μ˜ μ§„μ‹€μ˜ 근원)
  2. State is read-only (StateλŠ” μ½κΈ°μ „μš©μ΄λ‹€)
  3. Changes are made with Pure Functions (λ³€ν™”λŠ” 순수 ν•¨μˆ˜λ‘œ λ§Œλ“€μ–΄μ Έμ•Ό ν•œλ‹€)

redux λ“€μ–΄κ°€κΈ° μ „, κ°œλ… 미리보기

  1. μ•‘μ…˜ (Action)
    • λ³€ν™”κ°€ ν•„μš”ν•  λ•Œ, μš°λ¦¬λŠ” μ•‘μ…˜μ΄λž€ 것을 λ°œμƒμ‹œν‚¨λ‹€.
    • μ•‘μ…˜ κ°μ²΄λŠ” type ν•„λ“œλ₯Ό ν•„μˆ˜μ μœΌλ‘œ 가지고 μžˆμ–΄μ•Όν•œλ‹€.
  2. μ•‘μ…˜ μƒμ„±ν•¨μˆ˜ (Action Creator)
    • νŒŒλΌλ―Έν„°λ₯Ό λ°›μ•„μ™€μ„œ μ•‘μ…˜ 객체 ν˜•νƒœλ‘œ λ§Œλ“€μ–΄μ€€λ‹€.
  3. λ¦¬λ“€μ„œ (Reducer)
    • λ¦¬λ“€μ„œλŠ” λ‘κ°€μ§€μ˜ νŒŒλΌλ―Έν„°(state, action)λ₯Ό λ°›μ•„μ˜¨λ‹€.
    • λ¦¬λ“€μ„œλŠ”, ν˜„μž¬μ˜ μƒνƒœμ™€, 전달 받은 μ•‘μ…˜μ„ μ°Έκ³ ν•˜μ—¬ μƒˆλ‘œμš΄ μƒνƒœλ₯Ό λ§Œλ“€μ–΄μ„œ λ°˜ν™˜ν•œλ‹€.
  4. μŠ€ν† μ–΄ (Store)
    • μŠ€ν† μ–΄ μ•ˆμ—λŠ”, ν˜„μž¬μ˜ μ•± μƒνƒœμ™€, λ¦¬λ“€μ„œκ°€ λ“€μ–΄κ°€μžˆκ³ , μΆ”κ°€μ μœΌλ‘œ λͺ‡κ°€μ§€ λ‚΄μž₯ ν•¨μˆ˜λ“€μ΄ μžˆλ‹€.
  5. λ””μŠ€νŒ¨μΉ˜ (dispatch)
    • λ””μŠ€νŒ¨μΉ˜λŠ”, μ•‘μ…˜μ„ λ°œμƒ μ‹œν‚€λŠ” 것이닀. dispatch λΌλŠ” ν•¨μˆ˜μ—λŠ” μ•‘μ…˜μ„ νŒŒλΌλ―Έν„°λ‘œ μ „λ‹¬ν•œλ‹€.
    • κ·Έλ ‡κ²Œ ν˜ΈμΆœμ„ ν•˜λ©΄, μŠ€ν† μ–΄λŠ” λ¦¬λ“€μ„œ ν•¨μˆ˜λ₯Ό μ‹€ν–‰μ‹œμΌœμ„œ ν•΄λ‹Ή μ•‘μ…˜μ„ μ²˜λ¦¬ν•˜λŠ” 둜직이 μžˆλ‹€λ©΄ μ•‘μ…˜μ„ μ°Έκ³ ν•˜μ—¬ μƒˆλ‘œμš΄ μƒνƒœλ₯Ό λ§Œλ“€μ–΄μ€€λ‹€.

Architecture

  • 보톡 reduxλ₯Ό μ“Έ λ•Œ, μ•„λž˜μ™€ 같은 ꡬ쑰λ₯Ό μΆ”μ²œν•˜λŠ” 것 κ°™λ‹€.
.
β”œβ”€β”€ public
└── src
    β”œβ”€β”€ component
    β”œβ”€β”€ action
    β”œβ”€β”€ reducer
    └── store
  1. action 폴더
    • API 톡신과 같은 μž‘μ—…μ„ ν•˜λŠ” μ•‘μ…˜ λ©”μ„œλ“œ(action creator)λ₯Ό λͺ¨μ•„ λ‘” 폴더
    • μ•‘μ…˜ λ©”μ„œλ“œμ—μ„œλŠ” λ¦¬λ“€μ„œ(reducer)둜 데이터 생성을 μš”μ²­ν•œλ‹€.
  2. component 폴더
    • React μ»΄ν¬λ„ŒνŠΈλ‘œ κ΅¬μ„±λœ 폴더닀. μ»΄ν¬λ„ŒνŠΈλŠ” 보톡 λ„λ©”μΈλ³„λ‘œ ꡬ뢄돼 μžˆλ‹€.
  3. reducer 폴더
    • λ¦¬λ“€μ„œλŠ” μ•‘μ…˜ λ©”μ„œλ“œμ—μ„œ λ³€κ²½ν•œ μƒνƒœλ₯Ό λ°›μ•„ 기쑴의 μƒνƒœλ₯Ό μƒˆλ‘œμš΄ μƒνƒœλ‘œ λ³€κ²½ν•˜λŠ” 일을 ν•œλ‹€.
  4. store 폴더
    • storeν΄λ”μ—λŠ” index.js 파일 ν•˜λ‚˜λ§Œ 있으며, 주둜 미듀웨어λ₯Ό μ„€μ •ν•˜λŠ” 일을 ν•œλ‹€.

μ°Έκ³  링크: Redux둜의 카툰 μ•ˆλ‚΄μ„œ - bestalign’s dev blog

Clone this wiki locally