Skip to content

Commit 3c97680

Browse files
committed
Fix isDefaultPrevented and isPropagationStopped to not get nulled
This was a bug introduced by #5947. It's very confusing that they become nulled while stopPropagation/preventDefault don't.
1 parent ac71c29 commit 3c97680

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

packages/events/SyntheticEvent.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ function SyntheticEvent(
7070
delete this.nativeEvent;
7171
delete this.preventDefault;
7272
delete this.stopPropagation;
73+
delete this.isDefaultPrevented;
74+
delete this.isPropagationStopped;
7375
}
7476

7577
this.dispatchConfig = dispatchConfig;
@@ -180,8 +182,8 @@ Object.assign(SyntheticEvent.prototype, {
180182
this.dispatchConfig = null;
181183
this._targetInst = null;
182184
this.nativeEvent = null;
183-
this.isDefaultPrevented = null;
184-
this.isPropagationStopped = null;
185+
this.isDefaultPrevented = functionThatReturnsFalse;
186+
this.isPropagationStopped = functionThatReturnsFalse;
185187
this._dispatchListeners = null;
186188
this._dispatchInstances = null;
187189
if (__DEV__) {
@@ -190,6 +192,16 @@ Object.assign(SyntheticEvent.prototype, {
190192
'nativeEvent',
191193
getPooledWarningPropertyDefinition('nativeEvent', null),
192194
);
195+
Object.defineProperty(
196+
this,
197+
'isDefaultPrevented',
198+
getPooledWarningPropertyDefinition('isDefaultPrevented', functionThatReturnsFalse),
199+
);
200+
Object.defineProperty(
201+
this,
202+
'isPropagationStopped',
203+
getPooledWarningPropertyDefinition('isPropagationStopped', functionThatReturnsFalse),
204+
);
193205
Object.defineProperty(
194206
this,
195207
'preventDefault',

packages/react-dom/src/events/__tests__/SyntheticEvent-test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,58 @@ describe('SyntheticEvent', () => {
249249
expect(expectedCount).toBe(1);
250250
});
251251

252+
it('should warn when calling `isPropagationStopped` if the synthetic event has not been persisted', () => {
253+
let node;
254+
let expectedCount = 0;
255+
let syntheticEvent;
256+
257+
const eventHandler = e => {
258+
syntheticEvent = e;
259+
expectedCount++;
260+
};
261+
node = ReactDOM.render(<div onClick={eventHandler} />, container);
262+
263+
const event = document.createEvent('Event');
264+
event.initEvent('click', true, true);
265+
node.dispatchEvent(event);
266+
267+
expect(() => expect(syntheticEvent.isPropagationStopped()).toBe(false)).toWarnDev(
268+
'Warning: This synthetic event is reused for performance reasons. If ' +
269+
"you're seeing this, you're accessing the method `isPropagationStopped` on a " +
270+
'released/nullified synthetic event. This is a no-op function. If you must ' +
271+
'keep the original synthetic event around, use event.persist(). ' +
272+
'See https://fb.me/react-event-pooling for more information.',
273+
{withoutStack: true},
274+
);
275+
expect(expectedCount).toBe(1);
276+
});
277+
278+
it('should warn when calling `isDefaultPrevented` if the synthetic event has not been persisted', () => {
279+
let node;
280+
let expectedCount = 0;
281+
let syntheticEvent;
282+
283+
const eventHandler = e => {
284+
syntheticEvent = e;
285+
expectedCount++;
286+
};
287+
node = ReactDOM.render(<div onClick={eventHandler} />, container);
288+
289+
const event = document.createEvent('Event');
290+
event.initEvent('click', true, true);
291+
node.dispatchEvent(event);
292+
293+
expect(() => expect(syntheticEvent.isDefaultPrevented()).toBe(false)).toWarnDev(
294+
'Warning: This synthetic event is reused for performance reasons. If ' +
295+
"you're seeing this, you're accessing the method `isDefaultPrevented` on a " +
296+
'released/nullified synthetic event. This is a no-op function. If you must ' +
297+
'keep the original synthetic event around, use event.persist(). ' +
298+
'See https://fb.me/react-event-pooling for more information.',
299+
{withoutStack: true},
300+
);
301+
expect(expectedCount).toBe(1);
302+
});
303+
252304
it('should properly log warnings when events simulated with rendered components', () => {
253305
let event;
254306
function assignEvent(e) {

0 commit comments

Comments
 (0)