Explore Library
Code QuizAdvanced

Redux Store Never Updates

Spot why a SwiftUI Redux-style store fails to notify views after dispatching an action.

Codeswift
struct AppState {
    var count = 0
}

enum Action {
    case increment
}

func reducer(_ state: inout AppState, _ action: Action) {
    switch action {
    case .increment:
        state.count += 1
    }
}

final class Store: ObservableObject {
    @Published private(set) var state: AppState

    init(state: AppState = AppState()) {
        self.state = state
    }

    func dispatch(_ action: Action) {
        var newState = state
        reducer(&newState, action)
    }
}

This Redux-style store compiles, but SwiftUI views never update after dispatch. What is the bug?