Skip to content

Commit 1b00af6

Browse files
committed
Added a warning for adding a property in the SyntheticEvent object
1 parent 9c3f595 commit 1b00af6

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/renderers/dom/client/syntheticEvents/SyntheticEvent.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ var assign = require('Object.assign');
1717
var emptyFunction = require('emptyFunction');
1818
var warning = require('warning');
1919

20+
var didWarnAddedProperty = false;
21+
2022
/**
2123
* @interface Event
2224
* @see http://www.w3.org/TR/DOM-Level-3-Events/
@@ -157,14 +159,33 @@ assign(SyntheticEvent.prototype, {
157159
*/
158160
destructor: function() {
159161
var Interface = this.constructor.Interface;
160-
for (var propName in Interface) {
162+
var propName;
163+
for (propName in Interface) {
161164
this[propName] = null;
162165
}
163166
this.dispatchConfig = null;
164167
this._targetInst = null;
165168
this.nativeEvent = null;
166-
},
169+
this.isDefaultPrevented = null;
170+
this.isPropagationStopped = null;
167171

172+
if (__DEV__) {
173+
if (!didWarnAddedProperty) {
174+
for (propName in this) {
175+
if (this.hasOwnProperty(propName) && this[propName] != null) {
176+
warning(
177+
didWarnAddedProperty,
178+
'This synthetic event is reused for performance reasons. If you\'re ' +
179+
'seeing this, you\'re adding a property in the synthetic event object.' +
180+
'The property is never released. See ' +
181+
'https://fb.me/react-event-pooling for more information.'
182+
);
183+
didWarnAddedProperty = true;
184+
}
185+
}
186+
}
187+
}
188+
},
168189
});
169190

170191
SyntheticEvent.Interface = EventInterface;

src/renderers/dom/client/syntheticEvents/__tests__/SyntheticEvent-test.js

+15
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,19 @@ describe('SyntheticEvent', function() {
108108
'https://fb.me/react-event-pooling for more information.'
109109
);
110110
});
111+
112+
it('should warn if the synthetic event is added a property', function() {
113+
spyOn(console, 'error');
114+
var syntheticEvent = createEvent({});
115+
syntheticEvent.foo = 'foo';
116+
SyntheticEvent.release(syntheticEvent);
117+
expect(syntheticEvent.foo).toBe('foo');
118+
expect(console.error.calls.length).toBe(1);
119+
expect(console.error.argsForCall[0][0]).toBe(
120+
'Warning: This synthetic event is reused for performance reasons. If you\'re ' +
121+
'seeing this, you\'re adding a property in the synthetic event object.' +
122+
'The property is never released. See ' +
123+
'https://fb.me/react-event-pooling for more information.'
124+
);
125+
});
111126
});

0 commit comments

Comments
 (0)