Skip to content

Commit 017f692

Browse files
StringEpsilontimdorr
authored andcommittedApr 2, 2019
Fixed maximum update depth exceeded caused by Redirect. (#6674)
* Redirect: Failing test for rendering in functional component * Properly check if location matches in <Redirect/> Fixes #6673 * Redirect: Handle all eventualities of to strings. * Redirect: Add update depth test with 'to' as location * Redirect: Simplify and rename tests. * Redirect: work around locationsAreEquals quirk.
1 parent f9849c8 commit 017f692

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed
 

‎packages/react-router/modules/Redirect.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@ function Redirect({ computedMatch, to, push = false }) {
4343
method(location);
4444
}}
4545
onUpdate={(self, prevProps) => {
46-
if (!locationsAreEqual(prevProps.to, location)) {
46+
const prevLocation =
47+
typeof prevProps.to === "string"
48+
? createLocation(prevProps.to)
49+
: prevProps.to;
50+
if (
51+
!locationsAreEqual(prevLocation, {
52+
...location,
53+
key: prevLocation.key
54+
})
55+
) {
4756
method(location);
4857
}
4958
}}

‎packages/react-router/modules/__tests__/Redirect-test.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
33
import { MemoryRouter, Redirect, Route, Switch } from "react-router";
4-
4+
import { createLocation } from "history";
55
import renderStrict from "./utils/renderStrict";
66

77
describe("A <Redirect>", () => {
@@ -11,6 +11,31 @@ describe("A <Redirect>", () => {
1111
ReactDOM.unmountComponentAtNode(node);
1212
});
1313

14+
describe("that always renders", () => {
15+
it("doesn't break / throw when rendered with string `to`", () => {
16+
expect(() => {
17+
renderStrict(
18+
<MemoryRouter>
19+
<Redirect to="go-out" />
20+
</MemoryRouter>,
21+
node
22+
);
23+
}).not.toThrow();
24+
});
25+
26+
it("doesn't break / throw when rendered with location `to`", () => {
27+
const to = createLocation("/go-out?search=foo#hash");
28+
expect(() => {
29+
renderStrict(
30+
<MemoryRouter>
31+
<Redirect to={to} />
32+
</MemoryRouter>,
33+
node
34+
);
35+
}).not.toThrow();
36+
});
37+
});
38+
1439
describe("inside a <Switch>", () => {
1540
it("automatically interpolates params", () => {
1641
let params;

0 commit comments

Comments
 (0)
Please sign in to comment.