0

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.

1
  • 1
    Can you post your code please? history.push is correct, so you might just be calling it wrong. Commented Jul 10, 2018 at 9:28

2 Answers 2

2

<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')
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

this.props.history.push says undefined
could you provide a minimalist snippet with your component and how do you invoke the function / call to history.push ?
I can but the code has changed a lot since this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.