How @BindingState works? #2407
-
Just trying to get my head around how @BindingState works. In the example below, when the action isUsefulButtonTapped is sent to the reducer. If I initially set isUseful to nil every time isUsefulButtonTapped action is sent to the reducer. Will the binding to isUseful be nil until its later changed to false when all state changes have completed? Or is the final state change the only one observed struct SomeFeature: Reducer {
struct State: Equatable {
@BindingState var isUseful: Bool?
}
enum Action: Equatable, BindableAction {
case binding(BindingAction<State>)
case isUsefulButtonTapped
}
var body: some ReducerOf<Self> {
BindingReducer()
Reduce { state, action in
switch action {
case .isUsefulButtonTapped:
state.isUseful = nil // Initial state
// Some action
state.isUseful = true
return .none
case .binding:
return .none
}
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @Muhammed9991, can you explain what you expect to happen from doing something like this: case . isUsefulButtonTapped:
state.isUseful = nil
state.isUseful = true Do you expect the view store to emit 2 times for each of these changes? That is not how it works. A function with an But if you are talking about something else then can you please provide more information? |
Beta Was this translation helpful? Give feedback.
Hi @Muhammed9991, can you explain what you expect to happen from doing something like this:
Do you expect the view store to emit 2 times for each of these changes?
That is not how it works. A function with an
inout
parameter cannot observe every change made inside the function. It can only observe the final change. Also, thereduce
method is completely synchronous and nothing async can be done inside (that is what effects are for). So there doesn't seem to be a reason to observe each change on the inside.But if you are talking about something else then can you please provide more information?