Action => single effect or multiple actions & effects? #3377
Answered
by
mbrandonw
technicated
asked this question in
Q&A
-
What's the difference (and what are the implications) between the following two implementations? Please bear with me as I'm writing this directly in GitHub so there might be some errors! // first implementation
Reduce { state, action in
switch action {
case .onAppear:
return .run { send in
let firstData = try await apiClient.firstRequest()
await send(.firstDataSuccess(firstData))
let secondData = try await apiClient.secondRequest(firstData)
await send(.secondDataSuccess(secondData))
}
case let .firstDataSuccess(firstData):
// modify state
return .none
case let .secondDataSuccess(secondData):
// modify state
return .none
}
}
// second implementation
Reduce { state, action in
switch action {
case .onAppear:
return .run { send in
let firstData = try await apiClient.firstRequest()
await send(.firstDataSuccess(firstData))
}
case let .firstDataSuccess(firstData):
// modify state
return .run { send in
let secondData = try await apiClient.secondRequest()
await send(.secondDataSuccess(secondData))
}
case let .secondDataSuccess(secondData):
// modify state
return .none
}
} Thank you 🙏 |
Beta Was this translation helpful? Give feedback.
Answered by
mbrandonw
Sep 14, 2024
Replies: 1 comment 1 reply
-
Hi @technicated, either of these are completely fine and it's up to you which you want to use. The 2nd has the benefit of being able to use data from the newly modified state in order to make the second network request, but if that's not needed the first is completely fine. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
technicated
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @technicated, either of these are completely fine and it's up to you which you want to use. The 2nd has the benefit of being able to use data from the newly modified state in order to make the second network request, but if that's not needed the first is completely fine.