How to bind using BindingReducer to subset of the childstate using tca

41 views Asked by At

I am struggling with tca 'BindingReducer' and 'onChangeOf'.

I want to bind childState from ParentState, especially 'subset of the childState' changes.

Maybe there are several solutions, but i think 'BindingReducer and onChange' usage fits more for my situations.

But, i dont know how to use it of onChangeOf. I checked when childState changes, the onChangeOf code called. But the code of print() not called. imageLink

So I checked it using debug. When i set breakpoint on onChange code, it called. But the 51, 52 line not called.

I guess maybe forced-unwraping caused, so i erased it. But 51 line not called same.

I cant understand why this happened.. Sorry for newb newb...

The Code in ParentFeature using BindingReducer.

BindingReducer()
            .onChange(of: \.profileHeaderFeature) { oldValue, newValue in
                if let oldValue = oldValue, let newValue = newValue {
                    if oldValue.isShowingSpecializations != newValue.isShowingSpecializations {
                        
                    }
                }
            }

This is ChildFeature.

@Reducer
struct ProfileHeaderFeature {
    @ObservableState
    struct State: Equatable {
        var firstName: String
        var lastName: String
        var description: String
        var company: String
        var city: String
        var country: String
        var location: String
        var isShowingSpecializations: Bool = false
        var idxOfSpecialization: Int = -1
        var specializedFields: [String] = ["antman", "batman", "cat", "design", "develop", "hurk", "ironman", "spiderman", "superman"]
        init(firstName: String, lastName: String, description: String, company: String, city: String, country: String) {
            self.firstName = firstName
            self.lastName = lastName
            self.description = description
            self.company = company
            self.city = city
            self.country = country
            self.location = "\(city), \(country)"
        }
    }
    enum Action: BindableAction {
        case binding(BindingAction<State>)
        case specializedFieldClicked
        case specializedFieldElementChoiced(Int)
    }
    var body: some ReducerOf<Self> {
        BindingReducer()
        Reduce { state, action in
            switch action {
            case .binding(_):
                return .none
            case .specializedFieldClicked:
                state.isShowingSpecializations.toggle()
                return .none
            case .specializedFieldElementChoiced(let idx):
                state.idxOfSpecialization = idx
                state.isShowingSpecializations.toggle()
                return .none
            }
        }
    }
}

0

There are 0 answers