首页 > 解决方案 > 无法在 SwiftUI 中更新 Redux Flow 的全局状态

问题描述

我正在尝试在 SwiftUI 中实现 Redux 状态,并且在执行动作创建者之后我一直在更新全局状态。我迷失了在商店调度功能中做什么。

// Store.swift 

typealias ActionCreator = (_ dispatch: @escaping (Action) -> ()) -> ()

func getMovies() -> ActionCreator {
    return { dispatch in
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
            dispatch(.populateMovies([Movie(title: "ABC")]))
        }
    
    }
}

class Store: ObservableObject {
    
    var reducer: Reducer
    @Published private (set) var appState: AppState
    
    init(appState: AppState, reducer: Reducer) {
        self.appState = appState
        self.reducer = reducer
    }
    
    func dispatch(_ dispatch: ActionCreator) {
        // how to send the movies to the reducer to update the state 
        //self.reducer.update(&appState, dispatch)
    }
}



// ContentView: 

store.dispatch(getMovies()) 

标签: swiftswiftui

解决方案


推荐阅读