FaceBook μ΄ React λ₯Ό ꡬμΆν λ μκ°ν μν€ν μ²λ‘ λ°μ΄ν°μ νλ¦μ μμΈ‘ κ°λ₯νκ³ μ΄ν΄νκΈ° μ½κ² λ§λ€μ΄μ£Όλ λμμΈ ν¨ν΄
λκ·λͺ¨ μ΄ν리μΌμ΄μ μμ λ°μ΄ν° νλ¦μ νμ νκΈ°κ° μ΄λ ΅λ€! λ°μ΄ν° νλ¦μ μΌκ΄μ± μκ² κ΄λ¦¬νμ¬ μμΈ‘ κ°λ₯μ±μ λμ΄κΈ° μν¨!
- Model μ λ°μ΄ν°κ° λ³κ²½λλ©΄ View λ‘ μ λ¬νμ¬ νλ©΄μ μΆλ ₯
- View μμ λ°μ΄ν°λ₯Ό μ λ ₯λ°μ Model μ μ λ°μ΄νΈ
- iOS μ UIKit μ ViewController λ₯Ό μ€μ¬μΌλ‘ ν ν¨ν΄ ( MVC )
- ViewController κ° View lifeCycle μ μ§μ κ΄λ¦¬
- ViewController κ° λ무 ν μΌμ΄ λ§μμ§λ€β¦ ( λ€νΈμν¬ μμ², DB λ°μ΄ν° μμ², delegate, datasource )
π‘ μ¦, λ°μ΄ν°κ° μλ°©ν₯μΌλ‘ νλ₯΄κ² λλ€!
-
νλμ ViewController μ΄ λ§μ Model κ³Ό View λ₯Ό κ΄λ¦¬
-
볡μ‘ν μμ κ°μ
- Model λ°μ΄ν° λ³κ²½ β View μ λ°μ΄νΈ β λ λ€λ₯Έ Model μ μ λ°μ΄νΈ
- μλ‘ λ³΅μ‘νκ³ κ°ν μμ‘΄μ±μ κ°μ§κ² λ κ°λ₯μ± λμμ§
- μ΄λ¬ν μμ‘΄μ±μ΄ λ§μμ§μλ‘ μ΄λ ν μ΄λ²€νΈκ° λ°μνμ λ μ΄λ ν μν©μ΄ λ²μ΄μ§μ§ μμΈ‘ λΆκ°
- MVC μ λ¨μ μ΄μλ ViewController μ μν μ ViewModel λ‘ λΆλ¦¬ ( λ€νΈμν¬ μμ² λ± )
- ViewModel μ View μ μ΄λ²€νΈλ₯Ό κ°μ§νμ¬ μλ§λ λΉμ¦λμ€ λ‘μ§μ μν
- Model μ λ°μ΄ν°λ₯Ό κ°μ Έμ€κ±°λ μ λ°μ΄νΈ νκ³ View μ λ°μ΄ν°λ₯Ό μ λ°μ΄νΈ νμ¬ νλ©΄ μΆλ ₯
π‘ MVVM λ μ¬μ ν View μ ViewModel μ΄ μλ‘ λ°μ΄ν° λ°μΈλ©μ΄ λμ΄μμ΄ μλ°©ν₯ νλ¦μ κ°κ³ μλ€!
- μ¬μ©μμ μ λ ₯μ κΈ°λ°μΌλ‘ Action μ ꡬν
- Action μ Dispatcher μ μ λ¬νμ¬ Store (Model) μ λ³κ²½
- View μ λ³κ²½λ λ°μ΄ν°λ₯Ό λ°μ
-
Dispatcher
- Store κ° λ±λ‘ν΄ λμ Action νμ λ§λ€ νΈμΆν μ½λ°± ν¨μλ€μ΄ μ‘΄μ¬
- Action μ μ λ¬λ°μΌλ©΄ ν΄λΉνλ Store κ° Action νμ μ λ§λ μ½λ°± ν¨μλ₯Ό μ€ν
- Store μ λ°μ΄ν°λ₯Ό λ³κ²½νλ μμ μ μ€μ§ Dispatcher λ₯Ό ν΅ν΄ κ°λ₯
-
Store (Model)
- μν μ μ₯μλ‘ μνλ₯Ό λ³κ²½ν μ μλ λ©μλλ₯Ό κ°κ³ μμ
- μ΄λ€ Action μ΄ λ°μνλμ§μ λ°λΌ ν΄λΉνλ λ°μ΄ν° λ³κ²½μ μννλ μ½λ°±ν¨μλ₯Ό Dispatcher μ λ±λ‘
- λ§μ½ λ°μ΄ν°κ° λ³κ²½λλ©΄ View μ λ°μ΄ν°κ° λ³κ²½λμμμ μλ¦Ό
Ex. μ¦κ° λ²νΌμ΄ μλ Counter App
- TCA: Reducer , Flux: Dispatcher
import ComposableArchitecture
@Reducer
struct CounterFeature {
//...
enum Action {
case decrementButtonTapped
case incrementButtonTapped
}
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .decrementButtonTapped:
state.count -= 1
return .none
case .incrementButtonTapped:
state.count += 1
return .none
}
}
}
}
- TCA, Flux: Store
import ComposableArchitecture
@Reducer
struct CounterFeature {
@ObservableState
struct State {
var count = 0
}
// ...
var body: some ReducerOf<Self> {
// ...
}
}
- TCA, Flux: View
struct CounterView: View {
let store: StoreOf<CounterFeature>
var body: some View {
VStack {
Text("\(store.count)")
HStack {
Button("-") {
store.send(.decrementButtonTapped)
}
Button("+") {
store.send(.incrementButtonTapped)
}
}
}
}
}