3

So, Im trying to go to the route /forecast that i have defined among my routes. When trying to reach it with a link like this:

<Link to="/forecast">Test</Link>

Its working. But now Im trying to reach it programatically, after a button press, and Im having trouble understanding what to do in v4 of react-router. Ive tried this:

history.push('/forecast');

Which is what i gathered from the official documentation. But its not working, what am i supposed to do when trying to reach a route inside a function?

Andreas
  • 1,421
  • 3
  • 16
  • 32
  • Assuming the function is not inside a component used on , this has already been answered. http://stackoverflow.com/questions/42672842/how-to-get-history-on-react-router-v4 – Todd Chaffee Apr 08 '17 at 13:33
  • If your function is inside a component used in a , then the history object will be passed as properties to the component. https://reacttraining.com/react-router/web/api/Route/Route-props – Todd Chaffee Apr 08 '17 at 13:34
  • Thanks for the answers, but when Im trying to go to a location according to how I interpret the docs this.props.history.push('/forecast'); it says that history is undefined... – Andreas Apr 26 '17 at 06:07
  • You haven't provided enough code to reproduce the problem. https://stackoverflow.com/help/mcve – Todd Chaffee Apr 26 '17 at 12:33

0 Answers0