Optional Substate #91
Replies: 3 comments
-
We definitely wanna cover this use case in the future, but the simplest solution is to do an if let modalState = localStore.value.modalState {
let localStore = store.view { _ in modalState } It's also possible to define some handy helpers around this to make it a bit less awkward. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the answer! Didn't think about using the current value of the store. To work around this i could do something like this (but somehow it doesn't feel right): extension Store {
public func view<LocalValue, LocalAction>(
optionalValue toLocalValue: @escaping (Value) -> LocalValue?,
action toGlobalAction: @escaping (LocalAction) -> Action
) -> Store<LocalValue, LocalAction>? {
guard var localValue = toLocalValue(value) else { return nil }
return view(
value: {
localValue = toLocalValue($0) ?? localValue
return localValue
},
action: toGlobalAction)
}
} |
Beta Was this translation helpful? Give feedback.
-
Not necessarily the best solution, but something that can help relocate optionality from generic types onto concrete types: You can model "optionality" into concrete types like this: struct AppState {
var modalState: ModalState = .none //non-optional type
}
enum ModalState {
case none
case some(ActualModalState)
//you can add helper property or `CasePath` here
var actualModalState: ActualModalState? {
switch self {
case let .some(state):
return state
case .none:
return nil
}
}
struct ActualModalState {
let text: String
}
} now, in generic functions we are again dealing with non-optional types let localStore = store.view { $0.modalState } // Store<ModalState, AppAction> Clearly, this approach puts a burden of dealing with fake optionality on struct ModalView: View {
@ObservedObject var store: Store<ModalState, ModalAction>
public var body: some View {
self.store.state.actualModalState.map { state in
Text("\(state.text)")
}
}
You should still be able to drill down to actual modal state in view: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
i've really enjoyed the series about the composable architecture. Right now i'm applying it to a project and got stuck with a problem regarding optional substates.
Considering the following example:
The above
modalReducer
is now able to work with non-optional state which is great. But as soon i try to view into the store to present a modal dialog the following problem occurs:My goal is to have a local store that works on a non-optional state (Imagine the
ModalState
/modalReducer
are living in their own module and should therefore not have to deal with optionals).It would be great if you have any input about this kind of problem.
Beta Was this translation helpful? Give feedback.
All reactions