Skip to content

Commit 2200a45

Browse files
committed
Allow passing props through to renderRouteHandler when used manually (example attached)
1 parent 4e33401 commit 2200a45

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
var React = require('react');
3+
var Router = require('react-router-component');
4+
var TransitionGroup = require('react/lib/ReactCSSTransitionGroup');
5+
6+
/**
7+
* AnimatedLocations is an extension of the <Locations> object that animates route transitions.
8+
*/
9+
var AnimatedLocations = React.createClass({
10+
mixins: [
11+
Router.AsyncRouteRenderingMixin,
12+
Router.RouterMixin,
13+
React.addons.PureRenderMixin
14+
],
15+
16+
getDefaultProps: function() {
17+
return {
18+
component: 'div'
19+
}
20+
},
21+
22+
render: function() {
23+
// A key MUST be set in order for transitionGroup to work.
24+
var handler = this.renderRouteHandler({key: this.state.match.path});
25+
// TransitionGroup takes in a `component` property, and so does AnimatedLocations, so we pass through
26+
return <TransitionGroup {...this.props}>{handler}</TransitionGroup>;
27+
}
28+
});
29+
30+
module.exports = AnimatedLocations;

examples/extras/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### Extras
2+
3+
These components are not part of RRC and should not be imported into your modules.
4+
5+
Instead, use them as inspiration for what you want to build.

examples/extras/TitleSwitcher.jsx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
var React = require('react');
3+
var Router = require('react-router-component');
4+
5+
/**
6+
* TitleSwitcher is an extension of Router that switches the title on render.
7+
* The actual function that switches the title is not implemented here, as that
8+
* can be environment-specific in isomorphic apps.
9+
*/
10+
var TitleSwitcher = React.createClass({
11+
mixins: [
12+
Router.AsyncRouteRenderingMixin,
13+
Router.RouterMixin,
14+
React.addons.PureRenderMixin
15+
],
16+
17+
propTypes: {
18+
messages: React.PropTypes.object,
19+
component: React.PropTypes.node
20+
},
21+
22+
getDefaultProps: function() {
23+
return {
24+
component: 'div',
25+
messages: {}
26+
}
27+
},
28+
29+
getRoutes: function(props) {
30+
return props.children;
31+
},
32+
33+
setTitle: function() {
34+
var match = this.getMatch(), pageTitle;
35+
var path = match.route.path;
36+
if (path) {
37+
if (typeof path !== "string") {
38+
path = match.matchedPath;
39+
}
40+
// Assuming a messages object here. Can be used to change simple paths like '/account' into
41+
// "My Account", and for internationalization.
42+
var msg = this.props.messages.titles[path];
43+
pageTitle = typeof msg === "function" ? msg(match.match) : msg;
44+
} else {
45+
// Route not matched
46+
pageTitle = '404';
47+
}
48+
// Somewhere here you may want to set the page title
49+
// something.setPageTitle(pageTitle)
50+
return pageTitle;
51+
},
52+
53+
render: function() {
54+
var title = this.setTitle();
55+
var handler = this.renderRouteHandler();
56+
var Component = this.props.component || "div";
57+
return <Component {...this.props} data-page-title={title}>{handler}</Component>;
58+
}
59+
});
60+
61+
module.exports = TitleSwitcher;

lib/RouteRenderingMixin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
var React = require('react');
44
var cloneWithProps = require('react/lib/cloneWithProps');
5+
var assign = Object.assign || require('object.assign');
6+
57

68
/**
79
* Mixin for routers which implements the simplest rendering strategy.
@@ -10,8 +12,8 @@ var RouteRenderingMixin = {
1012

1113
renderRouteHandler: function() {
1214
var handler = this.state.handler;
15+
var props = assign({ref: this.state.match.route.ref}, props);
1316
// If we were passed an element, we need to clone it before passing it along.
14-
var props = {ref: this.state.match.route.ref};
1517
if (React.isValidElement(handler)) {
1618
return cloneWithProps(handler, props);
1719
}

0 commit comments

Comments
 (0)