Answer the question
In order to leave comments, you need to log in
How to change the url of a component when its state changes?
There is a page-component of an online store, which, by clicking on certain menu items, displays certain categories of products. There is only one component for displaying different categories, that is, it has one url in react-router. How to make it so that when the state of this component changes, the displayed url also changes?
I did so. Specified url value in state:
state = {
productType1: false,
productType2: false,
link: ' '
}
showProductType1= () => {
this.setState(() => {
return {
productType1: true,
productType2: false,
link: '/product_type1',
}
})
}
showProductType2= () => {
this.setState(() => {
return {
productType2: true,
productType1: false,
link: '/product_type2',
}
})
}
<Route exact path={this.state.link} render={(props) =>
<ProductList {...props} productType1={this.state.productType1} productType2={this.state.productType2} />}></Route>
<Navbar
showProductType1 = {this.showProductType1}
showProductType2 = {this.showProductType2}
link={this.state.link}/>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question