mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Added warning when calling methods on a released event.
This commit is contained in:
@@ -16,6 +16,7 @@ var PooledClass = require('PooledClass');
|
||||
|
||||
var assign = require('Object.assign');
|
||||
var emptyFunction = require('emptyFunction');
|
||||
var warning = require('warning');
|
||||
|
||||
/**
|
||||
* @interface Event
|
||||
@@ -89,6 +90,16 @@ assign(SyntheticEvent.prototype, {
|
||||
preventDefault: function() {
|
||||
this.defaultPrevented = true;
|
||||
var event = this.nativeEvent;
|
||||
if (__DEV__) {
|
||||
warning(event,
|
||||
'This Synthetic event is reused for performance reasons. If you\'re seeing this, ' +
|
||||
'you\'re calling `preventDefault` on a released/nullified Synthetic event. ' +
|
||||
'This is a no-op. See https://facebook.github.io/react/docs/events.html#event-pooling ' +
|
||||
'for more information.'
|
||||
);
|
||||
}
|
||||
if (!event) return;
|
||||
|
||||
if (event.preventDefault) {
|
||||
event.preventDefault();
|
||||
} else {
|
||||
@@ -99,6 +110,16 @@ assign(SyntheticEvent.prototype, {
|
||||
|
||||
stopPropagation: function() {
|
||||
var event = this.nativeEvent;
|
||||
if (__DEV__) {
|
||||
warning(event,
|
||||
'This Synthetic event is reused for performance reasons. If you\'re seeing this, ' +
|
||||
'you\'re calling `stopPropagation` on a released/nullified Synthetic event. ' +
|
||||
'This is a no-op. See https://facebook.github.io/react/docs/events.html#event-pooling ' +
|
||||
'for more information.'
|
||||
);
|
||||
}
|
||||
if (!event) return;
|
||||
|
||||
if (event.stopPropagation) {
|
||||
event.stopPropagation();
|
||||
} else {
|
||||
|
||||
@@ -72,4 +72,33 @@ describe('SyntheticEvent', function() {
|
||||
expect(syntheticEvent.isPersistent()).toBe(true);
|
||||
});
|
||||
|
||||
it('should warn if the Synthetic event has been released when calling `preventDefault`', function() {
|
||||
spyOn(console, 'error');
|
||||
var syntheticEvent = createEvent({});
|
||||
SyntheticEvent.release(syntheticEvent);
|
||||
syntheticEvent.preventDefault();
|
||||
expect(console.error.calls.length).toBe(1);
|
||||
expect(console.error.argsForCall[0][0]).toBe(
|
||||
'Warning: ' +
|
||||
'This Synthetic event is reused for performance reasons. If you\'re seeing this, ' +
|
||||
'you\'re calling `preventDefault` on a released/nullified Synthetic event. ' +
|
||||
'This is a no-op. See https://facebook.github.io/react/docs/events.html#event-pooling ' +
|
||||
'for more information.'
|
||||
);
|
||||
});
|
||||
|
||||
it('should warn if the Synthetic event has been released when calling `stopPropagation`', function() {
|
||||
spyOn(console, 'error');
|
||||
var syntheticEvent = createEvent({});
|
||||
SyntheticEvent.release(syntheticEvent);
|
||||
syntheticEvent.stopPropagation();
|
||||
expect(console.error.calls.length).toBe(1);
|
||||
expect(console.error.argsForCall[0][0]).toBe(
|
||||
'Warning: ' +
|
||||
'This Synthetic event is reused for performance reasons. If you\'re seeing this, ' +
|
||||
'you\'re calling `stopPropagation` on a released/nullified Synthetic event. ' +
|
||||
'This is a no-op. See https://facebook.github.io/react/docs/events.html#event-pooling ' +
|
||||
'for more information.'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user