Edit router 1 @ rc1 redirect to server

I need to redirect a page from another to the server using a react router. The code I wrote works on the client side, but not in the server rendering. you can find the code here:

https://github.com/jurgob/iso-login/blob/e26af0152896a949435db62549027b2683276db7/src/shared/components/LoginPage.js

this is the redirect code inside /src/shared/components/LoginPage.js:

componentWillMount() {
    ...
    this.props.history.replaceState(null, '/home');
  }

Note:

If you look at https://github.com/jurgob/iso-login/blob/e26af0152896a949435db62549027b2683276db7/src/shared/routes.js

I did:

function requireAuth(nextState, replaceState) {
  // replaceState({ nextPathname: nextState.location.pathname }, '/login');
}
...
<Route path="home" component={HomePage} onEnter={requireAuth} />

this code works, but I would like to do a redirect inside the component

+4
2

React Router .

, onEnter hooks match, React Router replaceState match . componentWillMount match .

, , history.replaceState , ReactDOM.renderToString.

+2

:

1 - , , -

history.push('/newPath/');

, , ( ). "" ( ,

componentWillMount() {
  if (this.props.context) { // context only available on server side
    this.props.context.doLogout();
  }
  this.props.history.push('/newPath/');
}

2 - , , , :

componentWillMount() {
  // you may have to this.props.history.push('/currentPath');
  this.props.res.redirect(302, '/newPath/');
}

- ( , , ).

+1

Source: https://habr.com/ru/post/1610551/


All Articles