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...