The easiest option would be storing the error in Redux and showing the alert in componentDidUpdate when you detect that it has changed:
componentDidUpdate (prevProps) {
if (!prevProps.error && this.props.error) {
// show the alert
}
}
Another option is invoking directly the alert directly from redux-thunk (I'm assuming you're using it for handling the fetching).
An even cleaner solution though (in my opinion) is achieved using something like redux-saga for triggering the side effect (the alert) when an error action is dispatched.
P.S.: Let me know if you need any example of the suggestions above :)
Sorry for being late! For sagas for example you can easily use something like this (from this post). For Redux thunk you can easily apply the same approach, just call a Alert.alert( 'Error', error) when you catch an exception and you want to display the Alert.
Matteo Mazzarolo
Engineer @invisionapp
Hi!
The easiest option would be storing the error in Redux and showing the alert in componentDidUpdate when you detect that it has changed:
componentDidUpdate (prevProps) { if (!prevProps.error && this.props.error) { // show the alert } }Another option is invoking directly the alert directly from redux-thunk (I'm assuming you're using it for handling the fetching).
An even cleaner solution though (in my opinion) is achieved using something like redux-saga for triggering the side effect (the alert) when an error action is dispatched.
P.S.: Let me know if you need any example of the suggestions above :)