1

In my current React app, i have a route like

<Route path="products" element={<Products />}>
     <Route path=":productId" element={<Product />} />
</Route>

in the Products component, I have an useEffect where I fetch all products. In the product component, I have another useEffect where I fetch a single product. Users can edit that product. in this component, a back button navigates the user to the "/products" route. But this time useEffectwas not triggered in the Products component so updated information on the Product component is not reflected here.

Arafat Ahmed
  • 145
  • 3
  • 10

1 Answers1

0

The useEffect inside Prodcuts will not be triggered because it is unaware of the change in the products.

You are fetching twice and each fetch is independent of the other as far as React is concerned. One solution is to store the results of the first fetch in state and then modify it in Product which will trigger useEffect.

Hasan Aga
  • 718
  • 8
  • 27