M
M
m_frost2019-06-12 12:07:29
React
m_frost, 2019-06-12 12:07:29

React-router Redirect previous location ??

There is a redirect from the product page for an unlogged user to the login page, I need to return it to the product page after login, I need props from the previous location so that if this is the location of the product, then do history.goBack() otherwise history.push(home). I know you can do this in links, but I didn’t find it in the redirect.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-06-12
@m_frost

Redirect:

<Redirect
  to={{
    pathname: '/login',
    state: { referrer: props.history.location.pathname }, // или `/products/${slug}`
  }}
/>

Login:
class Login extends React.Component {
  onLoginSuccess() {
    const { history } = this.props;
    const { state } = history.location;
    const location = (state && state.referrer) || '/';

    history.push(location);
  }

  /* ... *.
}

In addition to a link to the previous page in the state, you can write its scroll and restore it upon return.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question