How to replace catalog component with detailed item component using react router dom?

bread

I have a Main component for switching between my main components using routes.

const Main = () => {
  return (
    <Switch>
      <Route path="/" exact component={Landing} />
      <Route
        path="/catalog/:category/:type?/:typeOption?"
        component={Catalog}
      />
      <Route path="???" component={DetailedView} />
      />
    </Switch>
  );
};

The Catalog component is basically a list of items available in the store. When a user clicks on one of the items, I want to replace the Catalog component with a component that displays the item with all of its details. When i'm on the detailed page, the url should be the category i was on with the name of the item appended on to it.

For example - I might be on /catalog/mens/shirts/brown and when i click on an item, i would be sent to - /catalog/mens/shirts/brown/nameOfShirt. I can get the url to send the user to through the match prop, but what do i put for path of my DetailedView component?

Agney

It seems like you are looking for Nested Routing

Inside the main component, the switch would provide only the landing and catalog view.

<Switch>
  <Route path="/" exact component={Landing} />
  <Route
    path="/catalog/:category/:type?/:typeOption?"
    component={Catalog}
  />
</Switch>

Now further inside the catalog view, you can nest the detail with something like:

function Catalog({ match }) {
  return (
    <div>
      <Route
        path={match.path + '/:nameOfProduct'}
        component={DetailedView}
      />
      <Route
        exact
        path={match.path}
        render={() => <h3>Render the list of items here</h3>}
      />
    </div>
  )
}

This way, the URL is modified to appended to what there earlier.

To handle the optional parameters, you would have to differentiate the ID for type and product somehow. For eg. If I have a restriction that all product IDs would start with pr that would be like:

<Switch>
  <Route path={`${match.path}/:id(pr.*)`} render={() => <p>Product</p>} />
  <Route path={match.path} render={() => <p>Still Catalogue</p>} />
</Switch>

If you cannot differentiate them using a regex pattern, then you would have to add a routing parameter in the URL, making it:

/catalog/mens/shirts/product/nameOfShirt /catalog/mens/shirts/type/product/nameOfShirt

That way you can add the Route above the one for catalog in the Switch statement

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to add a route that will render a component using react-router-dom?

How to replace a react component with react-router

How can I replace bits or string with React component, react-router-dom Link or an anchor tag <a></a>

React router dom is not rendering a component

in react how to use reusable component as React Component to pass into react-router-dom Component prop?

Unable to access react component using react-router-dom

Cannot route to required React Component using React-Router-DOM

How to prevent a React component from unmounting when url path is matched using react-router-dom

How to call a component inside a component using React Router

How, using react-router-dom, can I direct a link to render a general post component, populated with props?

how to get match, location, history props in react router DOM 5 using component class?

How to get params in component in react router dom v4?

How to hide header component in react-router-dom?

React-Router-Dom 6 - How to dynamically render a component?

How to get parameters through component on react-router-dom-6

How to use useNavigate( ) React-Router-Dom Hook in Class Component

How to replace Link component behavior with History in React-Router

How to use React Router to replace a Component in render method?

Component not rendering using react router

React router dom not showing a component in the webpage

React Router Dom Link in another component

React router dom won't show component

React Router Dom, pass data to a component with useNavigate

react-router-dom not rendering my component

React Router DOM: Custom Route component not working

React router not rendering the component on click of a Array item

How can I pass the component's name from another component's object to Route in React Router Dom?

How to replace Panel Component with Card Component in React

How do you access url args in react-router-dom 4 when using Route render prop instead of Route component prop?