-
-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to set @State based on Flux state #11
Comments
Same/similar issue here, using When using a But how do I make this play nicely with a SwiftUIFlux? Problem is, that I can indeed kind of set the I spent almost the whole day, but didn't find a solution for that issue. But I have do admit, that I'm pretty new to SwiftUI, so may be there is a solution out there 🤞 Any clues, help are highly appreciated! |
Hi @d4rkd3v1l, probably the way you want to handle this is to embrace SwiftUI’s built-in components’ use of var body: some View {
Text("My View").sheet(isPresented: debugSheetBinding) {
DebugScreen()
}
}
var debugSheetBinding: Binding<Bool> {
.init(
get: { store.state.debugSheetIsVisible },
set: { store.dispatch(action: $0 ? ShowDebugSheet() : HideDebugSheet()) }
)
} Your method of accessing the app state and dispatching actions may vary (here I just have the store as an |
Thx @danhalliday this seems to work pretty well using the "@EnvironmentObject approach". Actually I went with the "ConnectedView approach" as this really feels more redux and is the "new preferred way" according to the docs here. I was just wondering, what would be a proper way to solve it using that approach. My problem here is, that I basically only have the "props" in the body function, but not for the binding. For sure I could just access the global store instance, but that's even less redux, and also I will try to encapsulate it somehow to not be able to globally reference it. Any ideas for that approach as well? |
I think that works, though I haven’t played with |
Thx @danhalliday (again) 🤣👌 Seems indeed to be working like follows. Not sure if I like the Binding init in
|
I give an example with possible clearer way with Combine struct SystemState: FluxState {
var checker = SystemChecker()
}
// @Published is available in class, so we created this
class SystemChecker: Codable {
// MARK: - Input
@Published var shouldShowLoginPopover = false
// MARK: - Output and will be called on onReceive
var shouldShowLoginPopoverPublisher: AnyPublisher<Bool?, Never> {
$shouldShowLoginPopover
.removeDuplicates()
.map { value -> Bool? in
value
}.eraseToAnyPublisher()
}
}
ZStack {
buildHomeView().edgesIgnoringSafeArea(.top)
}
.onReceive(props.systemChecker.shouldShowLoginPopoverPublisher, perform: { value in
self.showLoginPopover = value ?? false
}) |
For example, in order to display a list of movie timeline via a
Segmented Tab View
, I can a list of timeline viastore.state.moviesState.movies
, and also I can get the nearest future one as active/selected tab item.My question then is how I can set this value to my @State. Seem Flux like state is not designed for @State and @binding
The text was updated successfully, but these errors were encountered: