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

Redirect in route render #156

Closed
schickling opened this issue May 8, 2016 · 8 comments
Closed

Redirect in route render #156

schickling opened this issue May 8, 2016 · 8 comments

Comments

@schickling
Copy link

First of all thanks for actively maintaining this library! 👍

I was wondering what's currently the best practise to do a redirect when a Relay route returns an error?
A minimal workaround could look like this but there has to be a better way.

const render = ({ error, props, element }) => {
  if (error) {
    window.location.pathname = '/'
  }

  return React.cloneElement(element, props)
}

export default (
  <Route path='/'>
    <Route path=':id' component={RootView} queries={ViewerQuery} render={render} />
  </Route>
)
@taion
Copy link
Member

taion commented May 8, 2016

I'd render a dummy component (<RedirectOnMount to="/foo" />) that just redirects using context.router when it mounts.

@taion taion closed this as completed May 8, 2016
@schickling
Copy link
Author

schickling commented May 8, 2016

Thanks for the quick answer. That's how I already do it but that feels like a hack tbh...

@taion
Copy link
Member

taion commented May 8, 2016

A bit. Maybe I can inject router as a prop there. Or we can do remix-run/react-router#3325 (comment).

@schickling
Copy link
Author

I think having the router there would make a lot of sense since it save the additional render attempts.

@schickling
Copy link
Author

Do you think it make sense to reopen this in the meanwhile?

@taion taion reopened this May 8, 2016
@fabiosantoscode
Copy link

To fix this, I've created a Redirector component with https://www.npmjs.com/react-side-effect. It's working well, and it can be used wherever in the application.

@ramusus
Copy link

ramusus commented Aug 17, 2016

@fabiosantoscode where we can see your Redirector component?

@taion
Copy link
Member

taion commented Oct 29, 2016

React Router as-is doesn't offer the necessary hooks to do this the way I want. This functionality is doable with found; see relay-tools/found-relay#1.

@taion taion closed this as completed Oct 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants