Skip to content

Commit 2ce1d32

Browse files
MCAxiaztimdorr
authored andcommittedApr 6, 2019
call createLocation on 'to' regardless of type (#6690)
1 parent 10d78bb commit 2ce1d32

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed
 

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ function Redirect({ computedMatch, to, push = false }) {
4343
method(location);
4444
}}
4545
onUpdate={(self, prevProps) => {
46-
const prevLocation =
47-
typeof prevProps.to === "string"
48-
? createLocation(prevProps.to)
49-
: prevProps.to;
46+
const prevLocation = createLocation(prevProps.to);
5047
if (
5148
!locationsAreEqual(prevLocation, {
5249
...location,

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

+35-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe("A <Redirect>", () => {
2323
}).not.toThrow();
2424
});
2525

26-
it("doesn't break / throw when rendered with location `to`", () => {
26+
it("doesn't break / throw when rendered with location `to` created from string", () => {
2727
const to = createLocation("/go-out?search=foo#hash");
2828
expect(() => {
2929
renderStrict(
@@ -34,6 +34,40 @@ describe("A <Redirect>", () => {
3434
);
3535
}).not.toThrow();
3636
});
37+
38+
it("doesn't break / throw when rendered with object `to`", () => {
39+
const to = {
40+
pathname: "/path",
41+
state: {
42+
someState: "state"
43+
}
44+
};
45+
expect(() => {
46+
renderStrict(
47+
<MemoryRouter>
48+
<Redirect to={to} />
49+
</MemoryRouter>,
50+
node
51+
);
52+
}).not.toThrow();
53+
});
54+
55+
it("doesn't break / throw when rendered with location `to` created from object", () => {
56+
const to = createLocation({
57+
pathname: "/path",
58+
state: {
59+
someState: "state"
60+
}
61+
});
62+
expect(() => {
63+
renderStrict(
64+
<MemoryRouter>
65+
<Redirect to={to} />
66+
</MemoryRouter>,
67+
node
68+
);
69+
}).not.toThrow();
70+
});
3771
});
3872

3973
describe("inside a <Switch>", () => {

0 commit comments

Comments
 (0)
Please sign in to comment.