Hi Stack Overflow community!
I'm looking for a good practice and implementation to wait for a dispatched action in a component.
I know React-Native and Redux-Saga just say: don't wait for it, let the store be updated, and through selectors, your view will re-render.
All good and well, but how to handle things like this:
onPress = (object) => {
this.props.editObject(object).then(() => { //editObject is an action
NavigationService.navigateTo(otherScreen);
}).catch((error) => {
// Handle and show validation errors
})
}
Of course in the above example, this crashes, since there is no then or catch, since it is an action and not a promise... So how to realize such a flow?
The only solution I can come up with is to pass a string navigateTo to the action and navigate in the saga itself, which is not clean at all...
Any advice?
EDIT:
I still haven't found a good solution for this problem and I am getting more and more frustrated about the redux-saga middleware.
Let's give a more concrete example: I have a profile & image which needs to be updated (sequentially), in any normal js-framework this would be
try {
setLoading(true);
await updateProfile(...);
await updateProfileImage(...);
navigate(...)
} catch (e) {
//...
} finally {
setLoading(false);
}
To realize this with redux-saga, it would take me a new store (with profileUpdateDone, profileUpdateFailedStatus, profileImageUpdateDone and profileImageUpdateFailedStatus), a new reducer, new selectors, ...
- Do the action
- Update the store
- Listen in componentDidUpdate for profileUpdateDone or -FailedStatus
- Handle the error or do the second action
- Update the store
- Listen in componentDidUpdate for updateProfileImageDone or -FailedStatus
- Handle the error or navigate
The store is getting really dirty for adding all that crap. I could introduce a single store, reducer, actions and selectors per single component, but I got 58 components and already 7 stores to manage the global state of the app. It seems unmanageable to have 65 stores, 65 reducers, 65 selector files, etc...
I've been breaking my head into finding some kind of clean design pattern to fix this. It seems like the most basic of basic tasks that any JS-framework should be able to handle easily, but for some reason not in redux-saga.
Am I the only one facing this problem, or is redux-saga only meant for small apps with e.g. no more than 20 screens?