0

Goal: Retreive Id from URL

I have a feed of products with the following code:

  <Router>
        <Header />
        <Switch>
          <Route exact path="/" component={Feed} />
          <Route exact path="/" />
          <Route
            exact
            path="/product/:productId"
            component={DetailProductPage}
          />
        </Switch>
      </Router>

Here is my feed code:

  <div className="feed">
      <div className="feed__cards">
        {products.map((product) => (
          <ProductCard
            image={product.image}
            brand={product.brand}
            name={product.name}
            id={product.id}
          />
        ))}
      </div>
    </div>

And the ProductCard component that creates the URL:

<div className="productCard__container">
        <img src={image} />
        <h1>{brand}</h1>
        <h2>{name}</h2>
        <p>{id}</p>
        <p>{<a href={`/product/${id}`}>View</a>}</p>
</div>

When I click on an individual product, I'm redirected to the individual product page:

function DetailProductPage({ id }) {
  const productId = { id };

  return (
    <div>
      <h1>Product page</h1>
      <p>{id}</p>
    </div>
  );
}

When I go this route, I get a URL with the product parameter: (e.g., http://localhost:3000/product/6CeJGSm7dBTABs8rRBJh)

How do I query for the 6CeJGSm7dBTABs8rRBJh part of the URL and put it into a variable?

4 Answers 4

1

I assume you are using react-router-dom for this. There are a few ways you can retrieve this value from the URL, but the simplest approach with the provided code would be to use the useParams hook like so:

import { useParams } from 'react-router-dom';

function DetailProductPage({ id }) {
  const { productId } = useParams();

  return (
    <div>
      <h1>Product page</h1>
      <p>{productId}</p>
    </div>
  );
}

More info here: https://reactrouter.com/web/api/Hooks/useparams

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

Comments

0

Look like the follwoing code grabs the product id from the URL:

const params = useParams();

Comments

0

While exporting component, you need to use withRouter to get access to this parameter:

import { withRouter } from 'react-router-dom';
export default withRouter(ComponentName);

Your parameters are in props now. In you case:

const productId = props.match.params.productId;

or (using destructuring):

const { match: { params: { productId } } } = props;

Comments

0

You can parse the url by using the Url object provided by javascript.

To get your current location you can use

window.location

So by combining Url and location

let urlArr = new URL(window.location).href.split('/');
console.log(urlArr[urlArr.length - 1]);

This will log 6CeJGSm7dBTABs8rRBJh

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.