Skip to content

Commit 6723dc5

Browse files
committed
[added] ability to set params/query in Redirect
closes #275 closes #294
1 parent acc00f7 commit 6723dc5

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

docs/api/components/Redirect.md

+14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ to `*` so you can redirect anything not found to somewhere else.
1515

1616
The `name` of the route you want to redirect to.
1717

18+
### `params`
19+
20+
By default, the parameters will just pass through to the new route, but
21+
you can specify them if you need to (usually you shouldn't).
22+
23+
### `query`
24+
25+
By default, the query parameters will just pass through to the new
26+
route, but you can specify them if you need to (usually you shouldn't).
27+
1828
Example
1929
-------
2030

@@ -40,8 +50,12 @@ Example
4050

4151
<!-- `/get-in-touch` -> `/contact` -->
4252
<Redirect from="get-in-touch" to="contact" />
53+
4354
<!-- `/profile/123` -> `/about/123` -->
4455
<Redirect from="profile/:userId" to="about-user" />
56+
57+
<!-- `/profile/jasmin` -> `/about-user/123` -->
58+
<Redirect from="profile/jasmin" to="about-user" params={{userId: 123}} />
4559
</Routes>
4660
```
4761

modules/components/Redirect.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
var React = require('react');
22
var Route = require('./Route');
33

4-
function createRedirectHandler(to) {
4+
function createRedirectHandler(to, _params, _query) {
55
return React.createClass({
66
statics: {
77
willTransitionTo: function (transition, params, query) {
8-
transition.redirect(to, params, query);
8+
transition.redirect(to, _params || params, _query || query);
99
}
1010
},
1111

@@ -23,7 +23,7 @@ function Redirect(props) {
2323
return Route({
2424
name: props.name,
2525
path: props.from || props.path || '*',
26-
handler: createRedirectHandler(props.to)
26+
handler: createRedirectHandler(props.to, props.params, props.query)
2727
});
2828
}
2929

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var assert = require('assert');
2+
var expect = require('expect');
3+
var React = require('react/addons');
4+
var Route = require('../Route');
5+
var Redirect = require('../Redirect');
6+
var Routes = require('../Routes');
7+
8+
describe('a Redirect', function () {
9+
10+
it('redirects from old to new', function (done) {
11+
var descriptor = Redirect({ from: 'old', to: 'new' });
12+
13+
expect(descriptor.props.path).toEqual('old');
14+
15+
var fakeTransition = {
16+
redirect: function(to) {
17+
expect(to).toEqual('new');
18+
done();
19+
}
20+
};
21+
22+
descriptor.props.handler.willTransitionTo(fakeTransition);
23+
});
24+
25+
it('uses params and query from current path', function (done) {
26+
var descriptor = Redirect({ from: 'old', to: 'new' });
27+
var expectedParams = { foo: 'bar' };
28+
var expectedQuery = { baz: 'qux' };
29+
30+
var fakeTransition = {
31+
redirect: function(to, params, query) {
32+
expect(params).toEqual(expectedParams);
33+
expect(query).toEqual(expectedQuery);
34+
done();
35+
}
36+
};
37+
38+
descriptor.props.handler.willTransitionTo(fakeTransition, expectedParams, expectedQuery);
39+
});
40+
41+
it('uses params and query from the Redirect definition', function (done) {
42+
var expectedParams = { foo: 'bar' };
43+
var expectedQuery = { baz: 'qux' };
44+
var fakePathParams = { hooba: 'scooba' };
45+
var fakePathQuery = { doobie: 'scoobie' };
46+
47+
var descriptor = Redirect({
48+
from: 'old',
49+
to: 'new',
50+
params: expectedParams,
51+
query: expectedQuery
52+
});
53+
54+
var fakeTransition = {
55+
redirect: function(to, params, query) {
56+
expect(params).toEqual(expectedParams);
57+
expect(query).toEqual(expectedQuery);
58+
done();
59+
}
60+
};
61+
62+
descriptor.props.handler.willTransitionTo(fakeTransition, fakePathParams, fakePathQuery);
63+
});
64+
65+
});
66+

tests.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require('./modules/components/__tests__/DefaultRoute-test');
22
require('./modules/components/__tests__/Link-test');
33
require('./modules/components/__tests__/NotFoundRoute-test');
44
require('./modules/components/__tests__/Routes-test');
5+
require('./modules/components/__tests__/Redirect-test');
56

67
require('./modules/mixins/__tests__/ActiveContext-test');
78
require('./modules/mixins/__tests__/LocationContext-test');

0 commit comments

Comments
 (0)