Can you pattern match a binding with a key path and value in a reducer?

I have a binding reducer and would like to pattern match not just on setting a particular value but also one what I am setting it to.

I'm actually using the Focus Field example but would like to know when the $focusedField is set to nil. But I'm just not sure what to put in the binding pattern match.

I can do something like...

case .binding(\.$focusedField):
  // check what the value of the state.focusedField is and act accordingly.

But I'd like to do something like...

case .binding(\.$focusedField, value: nil):
  // just do the action I want when it's nil

Is that possible?

Thanks

You can use .binding(.set(\.$focusedField, nil)):
or even case .binding(\.$focusedField) where state.focusedField == nil:

4 Likes

Ah! Thanks! I'm sure I had tried that but I must have been off a bit. :smiley: