Шаг 1. Изменяем роутер:
<Switch>
<Route path={"/"} exact render={() => <div>'100'</div>} />
<Route path={"/male/hat"} exact component={ProductsList} />
<Route path={"/male/hat/:to"} exact component={ProductDetails} />
</Switch>
Шаг 2. Пишем компонент для детализации:
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>
);
}
Шаг 3. Изменяем компонент списка:
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>
);
};