Skip to content

Commit 0ff65cc

Browse files
committed
Merge pull request facebook#4635 from edvinerikson/event-pooling-warning
Added warning when calling methods on a released event.
2 parents f641f29 + 34af8f8 commit 0ff65cc

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

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

+21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var PooledClass = require('PooledClass');
1616

1717
var assign = require('Object.assign');
1818
var emptyFunction = require('emptyFunction');
19+
var warning = require('warning');
1920

2021
/**
2122
* @interface Event
@@ -89,6 +90,16 @@ assign(SyntheticEvent.prototype, {
8990
preventDefault: function() {
9091
this.defaultPrevented = true;
9192
var event = this.nativeEvent;
93+
if (__DEV__) {
94+
warning(event,
95+
'This Synthetic event is reused for performance reasons. If you\'re seeing this, ' +
96+
'you\'re calling `preventDefault` on a released/nullified Synthetic event. ' +
97+
'This is a no-op. See https://facebook.github.io/react/docs/events.html#event-pooling ' +
98+
'for more information.'
99+
);
100+
}
101+
if (!event) return;
102+
92103
if (event.preventDefault) {
93104
event.preventDefault();
94105
} else {
@@ -99,6 +110,16 @@ assign(SyntheticEvent.prototype, {
99110

100111
stopPropagation: function() {
101112
var event = this.nativeEvent;
113+
if (__DEV__) {
114+
warning(event,
115+
'This Synthetic event is reused for performance reasons. If you\'re seeing this, ' +
116+
'you\'re calling `stopPropagation` on a released/nullified Synthetic event. ' +
117+
'This is a no-op. See https://facebook.github.io/react/docs/events.html#event-pooling ' +
118+
'for more information.'
119+
);
120+
}
121+
if (!event) return;
122+
102123
if (event.stopPropagation) {
103124
event.stopPropagation();
104125
} else {

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

+29
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,33 @@ describe('SyntheticEvent', function() {
7272
expect(syntheticEvent.isPersistent()).toBe(true);
7373
});
7474

75+
it('should warn if the Synthetic event has been released when calling `preventDefault`', function() {
76+
spyOn(console, 'error');
77+
var syntheticEvent = createEvent({});
78+
SyntheticEvent.release(syntheticEvent);
79+
syntheticEvent.preventDefault();
80+
expect(console.error.calls.length).toBe(1);
81+
expect(console.error.argsForCall[0][0]).toBe(
82+
'Warning: ' +
83+
'This Synthetic event is reused for performance reasons. If you\'re seeing this, ' +
84+
'you\'re calling `preventDefault` on a released/nullified Synthetic event. ' +
85+
'This is a no-op. See https://facebook.github.io/react/docs/events.html#event-pooling ' +
86+
'for more information.'
87+
);
88+
});
89+
90+
it('should warn if the Synthetic event has been released when calling `stopPropagation`', function() {
91+
spyOn(console, 'error');
92+
var syntheticEvent = createEvent({});
93+
SyntheticEvent.release(syntheticEvent);
94+
syntheticEvent.stopPropagation();
95+
expect(console.error.calls.length).toBe(1);
96+
expect(console.error.argsForCall[0][0]).toBe(
97+
'Warning: ' +
98+
'This Synthetic event is reused for performance reasons. If you\'re seeing this, ' +
99+
'you\'re calling `stopPropagation` on a released/nullified Synthetic event. ' +
100+
'This is a no-op. See https://facebook.github.io/react/docs/events.html#event-pooling ' +
101+
'for more information.'
102+
);
103+
});
75104
});

0 commit comments

Comments
 (0)