3

Quick background - I have a React Redux application created using CRA CLI. There are 2 pages or routes set up in my App.js using BrowserRouter and Route of react-router-dom. One is /products which displays all products. Second is /product-details which shows details of a product.

Problem 1 - When I come to the products page, the redux state has all the products information. When I manually navigate to the url /product-details, the redux state with all the product information is lost. Why is this happening?

Problem 2 - Other than manual navigation, How do I programmatically navigate from /products to /product-details page without losing the redux state?

I have tried using the BrowserHistory as suggested in other SO answers. But that does not seem to work for both my above problems.

<Router>
    <div>
      <Route path="/" exact component={Products} />
      <Route path="/product-details" component={ProductDetails} />
    </div>
</Router>

Expected

  1. When I manually navigate to product-details from the address bar, the state from /products page should be available
  2. Should be able to programmatically navigate to the /product-details page and the state available.

4 Answers 4

3

1) Where did you get data from the server? maybe products page? If so, you can't preserve the data when you manually navigate to the detail page. In your case, in detail page, you should send a request to the server with the product ID to get product detail information. Then it will work for sure.

2) The Route for details page should look like this. <Route path="/product-details/:id" component={ProductDetails} /> And you can go to detail page like this. this.props.history.push('/product-details/1'); It will work for sure.

Sign up to request clarification or add additional context in comments.

Comments

3

It sounds like the way you're navigating is causing the new page to be fetched from the server. For example, if you change the URL manually in the address bar or set the URL with the browser's location API, that will trigger a page load. The way to navigate without triggering a new page load is by using react-router's history API from within your component like this:

this.props.history.push('/product-details');

Comments

2

You should use <Link> to navigate between page when you use router. Otherwise the page will be refreshed and your redux store will be reset. If you enter the url directly in browser and navigate to product-detail page it will not work.

<Link to="/product-details" />

1) When I manually navigate to product-details from the address bar, The state from /products page should be available

You need to fetch the product details again in this case. But you need product id for this. You can use the route /product-details/:id for this. Get the id in your product-details component and using fetch get the product details

2) Should be able to programmatically navigate to the /product-details page and the state available.

For mouse click use <Link>. For programatically use <Redirect>

Comments

0

I was struggling with the same issue but then i felt that the <a> anchor tag in pure html re-render my whole page which is the reason of losing all my states so tried <Link> tag of react-router-dom and it worked for me.

<Link to="/cart">Cart {cartProducts.length}</Link>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.