Skip to content

The reversed data flow from the standard SwiftUI views back to the AppState #59

Open
@mohammad19991

Description

@mohammad19991

Hi @nalexn

I like what you did with this clean-architecture approach, I read some of your articles related to SwiftUI State and I saw the below implementation of reversing the data flow from views to AppState:

extension Binding where Value: Equatable {
    func dispatched<State>(to state: Store<State>,
                           _ keyPath: WritableKeyPath<State, Value>) -> Self {
        return onSet { state[keyPath] = $0 }
    }
}

extension Binding {
    typealias ValueClosure = (Value) -> Void
    
    func onSet(_ perform: @escaping ValueClosure) -> Self {
        return .init(get: {
            self.wrappedValue
        }, set: {
            self.wrappedValue = $0
            perform($0)
        })
    }
}

where you will use it like this:

@State var routingState: Routing = .init()
private var routingBinding: Binding<Routing> {
    $routingState.dispatched(to: injected.appState, \.routing.home)
}

It works in terms of updating the AppState, but if we look at the implementation of dispatched/onSet function we notice that it wraps the Value in a new Binding instance and this will cause the view to rerender again while it's not needed, I wanna check if you are aware about this case or maybe I didn't understand the idea properly.

I can show you and example if needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions