Ayan Banerjee
Ayan Banerjee

Reputation: 151

Changing page in react from a function on click

I am using React-router and I want to change page from a function. The function is called onclicking a button in sidebar . I could do it with swapping buttons to a Link tag in react-router. However I want some styles to apply then change the page to render new component. SO how to change page from a function.

I have tried history.push and redirect in react router but both didnt work.

Upvotes: 0

Views: 6229

Answers (2)

Patrick Ferreira
Patrick Ferreira

Reputation: 2063

Colin is right history.push shall work. maybe called it with bad params.

You could read this part of the doc, you'll find more detail about it. https://reacttraining.com/react-router/core/api/location

Upvotes: 0

Joelgullander
Joelgullander

Reputation: 1684

<Link /> can be styled aswell, just append a className to it or pass in inline-styling.

If you want to call route in function you need to make sure your component is subscribed to the history prop either by referencing it in the component or passing it down. Then for example:

this.props.history.push('/some/path')

Upvotes: 2

Related Questions