I am building a react application using redux.
In the redux store, let's say I have a book object with title, isLoading, error:
const bookStore = {
title: null,
isLoading: false,
error: null
}
I have two actions:
- Fetch/GET book data from server
- Update/PUT book title
In each action, I have to handle isLoading and error.
In the react component I can check (we ignore the loading for now)
if (error) {return ErrorComponent}
else return BookComponent
The thing is that I want to show the errors in different ways. If the request is GET (fetch data), I want to display error as ErrorComponent. If the request is PUT (update title) I want to display the error as a Toast.
How can I do that if I only have one error property in redux store?
Should I write the code like:
const bookStore = {
title: null,
isLoading: false,
fetchError: null,
putError: null
}
But in this case I have to check like
if (fetchError) {display ErrorComponent}
else if (putError) {display Toast}
else display BookComponent
It is kind of inconvenient. What would be a better way to do this?