-
Hi all, I'm kind of stuck on how to work with more complicated shared state(nested or optional). Lets say: struct User {
let service: Service?
}
struct Service {
let array: [SomeModelWithID]
let array2: [SomeOtherModelWithID]
}
@Reducer
public struct ParentFeature {
@Reducer(state: .equatable, action: .equatable)
public enum Path {
case child(ChildFeature)
}
@ObservableState
public struct State: Equatable {
var path: StackState<ParentFeature.Path.State>
@Shared var user: User?
public init() {
_user = Shared(nil)
}
}
public enum Action: Equatable {
case entries(IdentifiedActionOf<ChildFeature>)
.......
then down in the reducer, I update user in some action and then have another action: case let .entries(.element(id: elementID, action: .delegate(.open))):
state.path.append(
.child(
ChildFeature.State(
sharedArray: // Here I need @Shared var that is User->Service->arrayElement->someArrayInsideElement
)
)
)
return .none Before I started to use shared states, I was able to do |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hi @jiledaa, you can still do this but it takes more steps due to the fact that optional chaining in Swift does not preserve writability. So when you do: $user.service?.array …you get only a Binding($user.service)?.array …to get a Then you order to capture the Binding(Binding($user.service)?.array[id: elementID]) This will give you a |
Beta Was this translation helpful? Give feedback.
Hi @jiledaa, I'm sorry there was a typo in those snippets. I meant
Shared
:The
Binding
type has similar initializers, andShared
is kind of like that TCA version ofBinding
, and so that's why I mixed them up.