Added warning when calling methods on a released event.
Edvin Erikson committed
Aug 15, 2015 at 21:26 UTC
34af8f8597b9aecc635cc82d41e8ea96793526a6
2 files changed
+50
src/renderers/dom/client/syntheticEvents/SyntheticEvent.js
+21
@@ -16,6 +16,7 @@ var PooledClass = require('PooledClass');
16
17
var assign = require('Object.assign');
18
var emptyFunction = require('emptyFunction');
19
+var warning = require('warning');
20
21
/**
22
* @interface Event
@@ -89,6 +90,16 @@ assign(SyntheticEvent.prototype, {
90
preventDefault: function() {
91
this.defaultPrevented = true;
92
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
+
103
if (event.preventDefault) {
104
event.preventDefault();
105
} else {
@@ -99,6 +110,16 @@ assign(SyntheticEvent.prototype, {
110
111
stopPropagation: function() {
112
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
+
123
if (event.stopPropagation) {
124
event.stopPropagation();
125
} else {
src/renderers/dom/client/syntheticEvents/__tests__/SyntheticEvent-test.js
+29
@@ -72,4 +72,33 @@ describe('SyntheticEvent', function() {
72
expect(syntheticEvent.isPersistent()).toBe(true);
73
});
74
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
+ });
104
});