I am working on a Redux app and I have come across a particular situation that I don't know how to handle.
Let's say I have a ProductsList
page: example.com/products
On this page, I am dispatching the getProducts
redux action and it calls the API and stores the products in the store.
And on this page, I list all the products. Now, the user clicks on a product and a new URL is pushed like: example.com/products/product/1
And on this ProductDetails
page, a redux action called setCurrentProduct
gets dispatched.
So far, it is good.
But the problem happens, when the user visits the URL example.com/products/product/1
directly.
In that case, the setCurrentProduct
action gets dispatched but it fails because there are no products in the store yet.
I think I can add an if-else statement to this ProductDetails
page and check if the products are not present in the store, then first dispatch the getProducts
and then setCurrentProduct
.
But I feel this is a hacky solution.
Maybe, I am wrong and this is the actual solution.
Can someone please guide me on whether this solution is correct, if not, then what is the correct solution.
PS: I am using NextJS.