0

I have created a Modal which is part of my Router configuration:

  return (
    <AppContext.Provider value={appContextValue}>
      <ProductLanding getProductModalTemplate={getProductModalTemplate} />
      <Switch location={isModal ? previousLocation : location}>
        <Route path="/cart">
          <Cart />
        </Route>
        <Route exact path="/product/:id">
          <Modal
            modalTemplate={
              productModalTemplate != null ? productModalTemplate : null
            }
            isModal={isModal}
          />
        </Route>
      </Switch>
      {isModal ? (
        <Route exact path="/product/:id">
          <Modal
            modalTemplate={
              productModalTemplate != null ? productModalTemplate : null
            }
            isModal={isModal}
          />
        </Route>
      ) : null}
    </AppContext.Provider>

I noticed when navigating via the browser to get to the route http://localhost:3001/product/3 the products array which is propagating Modal template is empty, so I added the following:

  if (!products.length) {
    console.log('products ', products);
     return null
  }

How can I fix this?

Any help would be appreciated!

Update:

I updated the <Switch/>

 <Switch location={isModal ? previousLocation : location}>
    <Route path="/cart">
      <Cart />
    </Route>
    <Route exact path="/product/:id">
      <Modal
        modalTemplate={
          productModalTemplate != null ? (
            productModalTemplate
          ) : (
            <div>Nothing</div>
          )
        }
        isModal={isModal}
      />
    </Route>
  </Switch>

So now when going to the route via the browser bar, the div Nothing gets rendered.

I know why! The way I have it the template is being propagated by the clicks in the ProductListing page:

  <div className={productClasses}>
        <LinkButton
          to={`/product/${id}`}
          onClick={() =>
            handleGetProductModalTemplate(<ProductModalTemplate />)
          }
        >
          <img className={styles.image} src={imageSrc} alt={title} />
        </LinkButton>
        <div className={styles.details}>
          <div className={styles.text}>
            <LinkButton
              to={`/product/${id}`}
              onClick={() =>
                handleGetProductModalTemplate(<ProductModalTemplate />)
              }
            >
              <h2 className={styles.title}>{title}</h2>{' '}
            </LinkButton>

            <span className={styles.price}>${finalPrice}</span>
          </div>

Nothing get rendered via the address bar because handleGetProductModalTemplate never fires!!! So my friends what would be the best way to handle this? I am sure the way would be React-Router. Like if this route is hit do this...

0

2 Answers 2

1

I guess the problem is due to exact parameter which you have passed in params of Route tag.

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

3 Comments

But isn't that what I should have? You mean <Route exact path="/product/:id">
I removed it. <Route path="/product/:id"> Didn't work. But thanks for your insight.
So, on console what you are getting ?
0

Can you please post your implementation of handleGetProductModalTemplate(). The way I would approach this sitation in my applications is I would use the useHistory() hook from React-Router to trigger the route change.

I would suggest you remove the to={/product/${id}} prop from the <LinkButton> component and instead pass the id value to the handleGetProductModalTemplate() function.

Perform your required logic inside handleGetProductModalTemplate() and then at the end trigger the route change.

let history = useHistory()

inside the handleGetProductModalTemplate() at the very end have this

history.push(`/product/${id}`)

This way you can ensure that handleGetProductModalTemplate() is being called before triggering the route change

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.