I am trying to set the url depending on the component that is rendered conditionally (following fb tutorial almost exactly). I skipped few details, but essentially code looks like this:
class ParentComponent extends React.Component {
render() {
const isTrue = this.state.isTrue;
let component= null;
if (isTrue) {
component = <Component1/>;
} else {
component= <Component2/>;
}
return (
<div>
{component}
<Component3/>
...
<ComponentN/>
</div>
);
}
}
Now, I want the url to be either /component1 or /component2 depending on which component is loaded. I've tried adding browserHistory.push('/component1'), but it doesn't seam to work.Also, I've tried this, but without success.
I am new to frontend development, so please correct me if I'm wrong. In my opinion, just pushing to the history can't solve the problem, because I actually want the page to stay on 'parent-component'at all times. But when one of the components (1 or 2) is loaded url should be updated. Hope this makes sense.
Versions:
npm -v react-router
5.2.0
Thanks!