Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I make the URL dynamic according to categories?

I’m creating a sidebar categories, wherein the users have choices to pick which categories he/she will pick.

The problem I’m encountering here is after clicking another another category, it adds in the url .

URL First click to Gadgets

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But after I click another button/category, this is what happen, example I click the gadget one more time. It appends on the url. How can I prevent this?

Clicking Gadget one more time

Sidebar.js

 <Box p={4} sx={{display: 'flex', alignItems:'center', justifyContent: 'center', gap: '10px'}}>
      <>
        {itemTables.map((item, i) => (
          <Link  key={item.name} style={{textDecoration: 'none'}} to={`products/${item.cat}`}>
            <Button variant="outlined">{item.name}</Button>
          </Link>
        ))}
      </>
   </Box>

ItemTables.js

export const itemTables =[
    {
        id: '1',
        name: "Comscie / IT Equipment",
        cat: 'gadgets'
    },
    {
        id: '2',
        name: "Nursing / Medication",
        cat: 'women'
    },
    {
        id: '3',
        name: 'Tourism / Clothes',
        cat: 'gadgets'
    },
    {
        id: '4',
        name: 'Health & Personal Care',
        cat: 'health'
    },

]

App.js

return(
  <BrowserRouter>
  <Routes>
    <Route exact path="/"  element={ <Home />} />


    {/* Auth */}
    <Route path="/register" element={ <Register />} />
    <Route index path="/login" element={ <Login />} />


    {/* Product */}
    <Route  path="/products/:category"  element={ <Home />} />
    <Route path="/sell" element={ <Sell  />} />
    <Route path="/myproducts/" element={ <MyProducts /> } />
    <Route path="/singleproduct/:id" element={<SingleProduct />} />
    <Route path="/editproduct/:id" element={<EditProduct />} />

  </Routes>
  <ToastContainer />
</BrowserRouter>
 )

>Solution :

<Link key={item.name} style={{textDecoration: 'none'}} to={`/products/${item.cat}`}>

This will help you because when you use products/${item.cat}, it will add the link just beside the previous link in the URL bar not replacing the link.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading