Skip to content

Commit 62ef78f

Browse files
committed
React v15.2.0 update: don't pass bad props to DOM elements
This won't be perfect every time (e.g. with `childProps`) but we can do some things to prevent bad props from being passed.
1 parent e6806f6 commit 62ef78f

File tree

6 files changed

+41
-12
lines changed

6 files changed

+41
-12
lines changed

lib/CaptureClicks.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ var urllite = require('urllite/lib/core');
55
var Environment = require('./environment');
66
var HashEnvironment = require('./environment/HashEnvironment');
77
var assign = Object.assign || require('object-assign');
8+
var omit = require('object.omit');
9+
10+
var PROP_TYPES = {
11+
component: React.PropTypes.func.isRequired,
12+
environment: React.PropTypes.object,
13+
gotoURL: React.PropTypes.func
14+
};
15+
16+
var PROP_KEYS = Object.keys(PROP_TYPES);
817

918
/**
1019
* A container component which captures <a> clicks and, if there's a matching
@@ -13,10 +22,7 @@ var assign = Object.assign || require('object-assign');
1322
var CaptureClicks = React.createClass({
1423
displayName: 'CaptureClicks',
1524

16-
propTypes: {
17-
component: React.PropTypes.func.isRequired,
18-
environment: React.PropTypes.object
19-
},
25+
propTypes: PROP_TYPES,
2026

2127
getDefaultProps: function() {
2228
return {
@@ -122,6 +128,7 @@ var CaptureClicks = React.createClass({
122128
var props = assign({}, this.props, {
123129
onClick: this.onClick
124130
});
131+
props = omit(props, PROP_KEYS);
125132
return this.props.component(props, this.props.children);
126133
}
127134

lib/Link.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var React = require('react');
44
var NavigatableMixin = require('./NavigatableMixin');
55
var Environment = require('./environment');
66
var assign = Object.assign || require('object-assign');
7+
var omit = require('object.omit');
78

89
/**
910
* Link.
@@ -78,6 +79,7 @@ var Link = React.createClass({
7879
onClick: this.onClick,
7980
href: this._createHref()
8081
});
82+
props = omit(props, ['global', 'globalHash']);
8183
return React.DOM.a(props, this.props.children);
8284
}
8385
});

lib/RouteRenderingMixin.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ var assign = Object.assign || require('object-assign');
99
*/
1010
var RouteRenderingMixin = {
1111

12+
propTypes: {
13+
childProps: React.PropTypes.object
14+
},
15+
1216
// Props passed at the `childProps` key are passed to all handlers.
1317
getChildProps: function() {
1418
var childProps = this.props.childProps || {};
@@ -25,7 +29,12 @@ var RouteRenderingMixin = {
2529
throw new Error("React-router-component: No route matched! Did you define a NotFound route?");
2630
}
2731
var handler = this.state.handler;
28-
var matchProps = this.state.matchProps;
32+
var isDOMElement = typeof handler.type === 'string';
33+
34+
// If this is a DOM element, don't send these props. This won't prevent all
35+
// warnings in 15.2.0, but it will catch a lot of them.
36+
var matchProps = isDOMElement ? null : this.state.matchProps;
37+
2938
var outProps = assign({ref: this.state.match.route.ref}, this.getChildProps(), matchProps);
3039

3140
// If we were passed an element, we need to clone it before passing it along.

lib/Router.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ var React = require('react');
44
var RouterMixin = require('./RouterMixin');
55
var RouteRenderingMixin = require('./RouteRenderingMixin');
66
var assign = Object.assign || require('object-assign');
7+
var omit = require('object.omit');
8+
9+
// These are keys to omit - useful for preventing 15.2.0 warning regarding unknown props on DOM els
10+
var PROP_KEYS = ['component']
11+
.concat(Object.keys(RouterMixin.propTypes))
12+
.concat(Object.keys(RouteRenderingMixin.propTypes));
713

814
/**
915
* Create a new router class
@@ -19,6 +25,13 @@ function createRouter(name, component) {
1925

2026
displayName: name,
2127

28+
propTypes: {
29+
component: React.PropTypes.oneOfType([
30+
React.PropTypes.string,
31+
React.PropTypes.element
32+
])
33+
},
34+
2235
getRoutes: function(props) {
2336
return props.children;
2437
},
@@ -39,9 +52,7 @@ function createRouter(name, component) {
3952
// Pass all props except this component to the Router (containing div/body) and the children,
4053
// which are swapped out by the route handler.
4154
var props = assign({}, this.props);
42-
delete props.component;
43-
delete props.children;
44-
delete props.childProps;
55+
props = omit(props, PROP_KEYS);
4556
return React.createElement(this.props.component, props, handler);
4657
}
4758
}

lib/matchRoutes.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var invariant = require('./util/invariant');
55
var warning = require('./util/warning');
66
var React = require('react');
77
var assign = Object.assign || require('object-assign');
8+
var omit = require('object.omit');
89
var qs = require('qs');
910

1011
var patternCache = {};
@@ -128,9 +129,7 @@ Match.prototype.getProps = function() {
128129
props._query = this.query || EMPTY_OBJECT;
129130

130131
// Delete props that shouldn't be passed to the handler.
131-
delete props.pattern;
132-
delete props.path;
133-
delete props.handler;
132+
props = omit(props, ['pattern', 'path', 'handler']);
134133

135134
return props;
136135
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "index.js",
66
"dependencies": {
77
"object-assign": "^4.1.0",
8+
"object.omit": "^2.0.0",
89
"qs": "^6.2.0",
910
"url-pattern": "~1.0.1",
1011
"urllite": "~0.5.0"
@@ -69,4 +70,4 @@
6970
"publishConfig": {
7071
"registry": "https://registry.npmjs.org"
7172
}
72-
}
73+
}

0 commit comments

Comments
 (0)