How to execute an action conditionally? #1919
Unanswered
Jerry-Wang-Developer
asked this question in
Q&A
Replies: 2 comments 1 reply
-
For this case, I usually store the struct Child: ReducerProtocol {
struct State: Equatable {
var isFavorite: Bool
}
enum Action {
case favorite(Bool)
}
@Dependency(\.isLoggedIn) var isLoggedIn
var body: some ReducerProtocol<State, Action> {
Reduce { state, action in
switch action {
case let .favorite(isFavorite): // ②
guard isLoggedIn else { return .none }
state.isFavorite = isFavorite
return .none
}
}
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
I think you can delegate the logic ② to its parent to execute instead of executing in the child |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Look forward for best practice for below scenario.
Let say I have a Root state holding login information and some child state.
For child state, some action need to perform after login. that is, if the user is not login, we need to show the login page for user to login, and after login.
The below is a very simple codes.
When Child View button click, The code execution is always ③ ① ② , But for my scenario, I want ③ ① execute execept ② when user is not login, How can I do that?
If can't, What is the best practice to handle this scenario. (I know the key problem is that how can I share some state(here is isLogin) among the app, Think about that will be many child states and in there lots of action need user login first to forward process.)
Beta Was this translation helpful? Give feedback.
All reactions