I am building my first React app.
In my code, I redirect to another page (a component) using
browserHistory.push(pathToMyComponent)
I also have tried out the react-router Link-element. The Link element enables me to pass data to the target component without having it showing up in the URL, like this:
<Link to={`/myComponent/${someData}`}>
But now I don't want to create a link. Instead I want to perform some operations when pushing a button, and then redirect with some calculated data.
How do I do this?
Edit:
Here is my code. In loginpage, the user can perform a facebook login. What I want is to redirect the user to the lobby after login succeeded. I want to pass the userid to the lobby.
<Router history={browserHistory} onUpdate={() => window.scrollTo(0, 0)}>
<Route path=/ component={LoginPage}/>
<Route path=/lobby component={Lobby}/>
</Router>
This is what I wrote after reading your answer. When this is executed, the log prints Uncaught TypeError: Cannot read property 'context' of null
this.context.router.push({ //browserHistory.push should also work here
pathname: '/lobby',
state: {userid: authData.uid}
});
Edit2: You mean like this? It gives syntax error.
class LoginPage extends React.Component {
contextTypes = {
router: React.PropTypes.object
};
constructor(props){
super(props);
...
...