I have a question related to react-router and class components in React this is the first time I use class components in an app and I kind of get confused with lifecycle methods
my issue is I have a component where I display products in this component I switch categories of products where I do filter when switching as the API doesn't provide each category with its product
so now I switch using react-router and I do change the route to "/category/:nameOfCategory"
when I click on the link of tech products in the address bar it changes but the component doesn't re-render as the props are not updating or refreshing
this is where I do the request
componentDidMount() {
request("http://localhost:4000/", getItems).then((ItemsData) => {
if (this.props.match.params.name === "all") {
this.setState({ products: ItemsData.category.products });
} else {
const filtered = ItemsData.category.products.filter((products) => {
let filteredProducts;
if (products.category === this.props.match.params.name) {
filteredProducts = products;
}
return filteredProducts;
});
this.setState({ products: filtered });
}
});
}
render() {
const { match, location } = this.props;
console.log(this.props);
return (
<div>
<h1 className="category-heading">{match.params.name}</h1>
<div className="category-items">
{this.state.products.map((product) => {
return <ItemCard key={location.key} productData={product} />;
})}
</div>
</div>
);
}
}
This is the route in App.js
<Router>
<Navbar
/>
<Switch>
<Route
exact
path="/"
>
<Redirect to="/category/all" />
</Route>
<Route path="/category/:name" component={CategoryPage}/>
</Switch>
</Router>