Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add <Forward /> to API #6070

Closed
du5rte opened this issue Apr 6, 2018 · 1 comment
Closed

Add <Forward /> to API #6070

du5rte opened this issue Apr 6, 2018 · 1 comment

Comments

@du5rte
Copy link

du5rte commented Apr 6, 2018

Often you redirect a route, as your application gets more complex and you require params and search on that route you find after hours of debugging that <Redirect /> strips everything away #4919. I propose a <Forward /> to explicitly say pass everything over, here's my current solution to this problem.

class Forward extends React.Component {
  forwardParams(to, computedMatch) {
    const paramKeys = Object.keys(computedMatch.params);

    const replacedParamsKeys = paramKeys.reduce((url, key) => (
      url.replace(`:${key}`, computedMatch.params[key])
    ), to);

    return replacedParamsKeys;
  }

  render() {
    const { to, computedMatch, location, ...props } = this.props;

    return (
      <Redirect
        to={{
          state: { from: location },
          pathname: this.forwardParams(to, computedMatch),
          search: location.search,
        }}
        {...props}
      />
    );
  }
}

Example usecase redirecting a authentication callback

<Forward from="/callback" to="/oauth/callback" />
@timdorr
Copy link
Member

timdorr commented Apr 7, 2018

This is already in 4.3.0-rc.2. It's on react-router@next.

@timdorr timdorr closed this as completed Apr 7, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jun 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants