On view modifiers that accept callbacks

It is quite easy to hookup view modifiers to TCA if said modifier accepts callback of type (A) -> ()

How is it possible to hookup view modifiers to TCA if modifier accepts callback of type (A) -> B? Can I send the event with A and return a value of type B from the state?

This is how I image it happening:

  .someModifier(perform: { instanceOfA in
    viewStore.send(.someActionAccepting(instanceOfA)
    return viewStore.instanceOfB
  })

The fragment of reducer might look like this:

case .someActionAccepting(let instanceOfA):
  state.instanceOfB = someCalc(instanceOfA)
  return .none

Would store have already spun through and updated the value of instanceOfB in reducer when action .someActionAccepting arrived?

Yes, exactly. Because Store processes actions synchronously the state is updated immediately after an action is sent, and therefore it's safe to access state from the view store right after sending an action. This is even how we suggest people handle certain SwiftUI and UIKit APIs that require returning data immediately (for example this GitHub discussion).

1 Like