How to ForEach on optional state?

Using the Todos example in TCA, if I make the todos array optional, I'm struggling to figure out how I then use the ForEach with that new optional array. I can't find a combination that works.

So in AppState I just made todos optional:

  var todos: IdentifiedArrayOf<Todo>?

So the compiler shows this error for the reducer:

It seems the compiler has a hard time choosing the correct ForEach, and I noticed that the ForEach I need already does a return self.optional.reducer(....

This seems to work, but feels too verbose, am I missing something obvious?

todoReducer
    .forEach(
        state: \.self,
        action: .self,
        environment: { $0 }
    )
    .optional
    .pullback(
        state: \.todos,
        action: /AppAction.todo(id:action:),
        environment: { _ in TodoEnvironment() }
    )

@pcolton That's probably the most straightforward way to handle his kind of double-nesting of state. We're working on some ways of improving the ergonomics of optional and enum-based state, though, that will hopefully clean this kind of thing up in the future!

Are there any updates on how best to handle this?

Terms of Service

Privacy Policy

Cookie Policy