M
M
miliko mikoyan2019-04-22 00:23:57
React
miliko mikoyan, 2019-04-22 00:23:57

How to open single product details using React Router?

I have a Nav component, it has two links. The first link leads to the main page, and the second one opens the Hat component. In turn, it sends a request to the server and receives an array of objects representing a list of products that, when clicked, should open a page with the price of this product. my code

const Nav = () => {
  return (
    <Router>
      <NavLink to={"/"}>{"Foo"}</NavLink>
      <NavLink to={"/male/hat"}>{"hat"}</NavLink>
      <Switch>
        <Route path={"/"} exact render={() => <div>'100'</div>} />
        <Route path={"/male/:hat"} exact render={() => <Hat />} />
      </Switch>
    </Router>
  );
};

const Hat = () => {
  const [arrProd, setArrProd] = useState([]);
  useEffect(() => {
    (async () => {
      await fetch(`https://foo0022.firebaseio.com//male/hat.json`)
        .then(res => res.json())
        .then(arr => setArrProd(Object.values(arr).flat()));
    })();
  }, []);
  console.log(arrProd);
  return (
    <Fragment>
      {arrProd.map(({ to, id, price, title }) => (
        <Fragment key={id}>
          <br />
          <NavLink to={`/male/hat/${to}`}>{title}</NavLink>
          <Route
            exact
            path={`/male/hat/${to}`}
            render={() => <div>{price}</div>}
          />
        </Fragment>
      ))}
    </Fragment>
  );
};

Suppose I click on the first link, I expect to see only product prices and Foo hat links. I can't write this code, but I can show in the picture

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-04-22
@miliko0022

Step 1. Change the router:

<Switch>
  <Route path={"/"} exact render={() => <div>'100'</div>} />
  <Route path={"/male/hat"} exact component={ProductsList} />
  <Route path={"/male/hat/:to"} exact component={ProductDetails} />
</Switch>

Step 2. Writing a component for detailing:
const ProductDetails = ({ match }) => {
  const { to } = match.params;
  const [state, setState] = useState({ isFetching: true, hat: null });
  const { isFetching, product } = state;  

  useEffect(() => {
    (async () => {
      await fetch(`https://foo0022.firebaseio.com//male/hat.json`)
        .then(res => res.json())
        .then(data => {
          const product = data.find(item => item.to === to);
          setState({ isFetching: false, product });
        });
    })();
  }, []);
  
  if (isFetching) return <div>...Loading</div>;

  if (!product) return <div>404: Product not found</div>;

  return (
    <div>
      <h1>{product.title}</h1>
      <h2>{product.price}</h2>
    </div>
  );
}

Step 3. Change the list component:
const ProductsList = () => {
  const [state, setState] = useState({ isFetching: true, products: [] });
  const { isFetching, products } = state;

  useEffect(() => {
    (async () => {
      await fetch(`https://foo0022.firebaseio.com//male/hat.json`)
        .then(res => res.json())
        .then(data => setState({ isFetching: false, products: data });
    })();
  }, []);
  
  if (isFetching) return <div>...Loading</div>;
  
  return (
    <ul>
      {products.map(({ to, title }) => (
          <li key={to}>
            <Link to={`/male/hat/${to}`}>{title}</Link>
          </li>
      ))}
    </ul>
  );
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question