1

I'm trying to add a bunch of redirects given an array of old routes and new routes so I came out with this component:

const redirectUrls = [
    { oldUrl: '/robin', newUrl: '/users' },
    { oldUrl: '/batman', newUrl: '/courses' }
];

export default Redirects = () => (
    redirectUrls.map((url, index) => <Route key={index} exact path={url.oldUrl} render={() => <Redirect to={url.newUrl} />} />)
);

Which works fine, but I wanted it to be simplier so I removed the Route and left only the redirect like this:

export default Redirects = () => (
    redirectUrls.map((url, index) => <Redirect key={index} exact from={url.oldUrl} to={url.newUrl} />))

But it won't work, all the redirects take me to the last route, which in this case is /courses, and its weird because if I do this:

<Switch>
    ...
    <Route path='/users' exact component={users} />
    <Route path="/courses" exact component={CoursesList} />
    <Redirect from='/robin' to='/users' />
    <Redirect from='/batman' to='/courses' /> // having them like this works fine
</Switch>

So it makes no sense for them fail when I create them with a map and I haven't found anything that leads to the cause nor to a solution.

1
  • can you show how your routes looks like in your react dev tool ? a screenshot may be Commented Apr 26, 2020 at 18:18

2 Answers 2

2

You need to wrap the exported redirects in a switch:

const Redirects = () => {
  return (
    <Switch>
      {redirectUrls.map(url => (
        <Redirect from={url.oldUrl} to={url.newUrl} />
      ))}
    </Switch>
  );
};

export default Redirects;

That's only if you actually need to make a component out of them. It's not totally clear what you need and are looking for since export default Redirects = () => is invalid syntax. If you just want to bundle them as some variable you can do:

export const Redirects = redirectUrls.map(url => <Redirect from={url.oldUrl} to {url.newUrl} />);

// App.js

<Switch>
    <Route path='/users' exact component={users} />
    <Route path="/courses" exact component={CoursesList} />
    ...
    {Redirects}
</Switch>
Sign up to request clarification or add additional context in comments.

2 Comments

Why adding another Switch to the redirects makes the map work? If having them one by one with one switch works fine. Also this export default Redirects = () is valid, even though the Redirects is unnecessary.
To be honest I'm not totally sure. I've used this as a workaround in the past but after playing with it some more it doesn't always behave as I expected it to. For instance, putting in a similar component of mapped Routes (no Switch) above the mapped Redirects component makes the redirects stop working. Placing the Redirects component at the top of the parent switch also breaks the rest of the routes for me. I'm not sure I can recommend using this method anymore, at least not without some clarification - the github issues page might be a good place to ask.
0

Did you try this with exact props

<Switch>
  ...
    <Route path='/users' exact component={users} />
    <Route path="/courses" exact component={CoursesList} />
    <Redirect exact from='/robin' to='/users' />
    <Redirect exact from='/batman' to='/courses' /> // having them like this works fine
</Switch>

Reference https://github.com/ReactTraining/react-router/issues/4837

1 Comment

Yeap I did try it, that code you posted actually works the issue is when I do it with a map

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.