Skip to content

Commit 98ce535

Browse files
authored
[RN] Expose event as a global variable during dispatch (facebook#35913)
## Summary This PR updates the event dispatching logic in React Native to expose the dispatched event in the global scope as done on Web (https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke) and in the new implementation of `EventTarget` in React Native (https://github.com/facebook/react-native/blob/d1b2ddc9cb4f7b4cb795fed197347173ed5c4bfb/packages/react-native/src/private/webapis/dom/events/EventTarget.js#L372). ## How did you test this change? Added unit tests
1 parent a48e9e3 commit 98ce535

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/react-native-renderer/src/__tests__/ReactFabric-test.internal.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,8 @@ describe('ReactFabric', () => {
10991099
// Check for referential equality
11001100
expect(ref1.current).toBe(event.target);
11011101
expect(ref1.current).toBe(event.currentTarget);
1102+
1103+
expect(global.event).toBe(event);
11021104
}}
11031105
onStartShouldSetResponder={() => true}
11041106
/>
@@ -1110,6 +1112,8 @@ describe('ReactFabric', () => {
11101112
// Check for referential equality
11111113
expect(ref2.current).toBe(event.target);
11121114
expect(ref2.current).toBe(event.currentTarget);
1115+
1116+
expect(global.event).toBe(event);
11131117
}}
11141118
onStartShouldSetResponder={() => true}
11151119
/>
@@ -1123,6 +1127,9 @@ describe('ReactFabric', () => {
11231127
const [dispatchEvent] =
11241128
nativeFabricUIManager.registerEventHandler.mock.calls[0];
11251129

1130+
const preexistingEvent = {};
1131+
global.event = preexistingEvent;
1132+
11261133
dispatchEvent(getViewById('one').instanceHandle, 'topTouchStart', {
11271134
target: getViewById('one').reactTag,
11281135
identifier: 17,
@@ -1150,7 +1157,9 @@ describe('ReactFabric', () => {
11501157
changedTouches: [],
11511158
});
11521159

1153-
expect.assertions(6);
1160+
expect(global.event).toBe(preexistingEvent);
1161+
1162+
expect.assertions(9);
11541163
});
11551164

11561165
it('propagates timeStamps from native events and sets defaults', async () => {

packages/react-native-renderer/src/legacy-events/EventPluginUtils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ function validateEventDispatches(event) {
6767
*/
6868
export function executeDispatch(event, listener, inst) {
6969
event.currentTarget = getNodeFromInstance(inst);
70+
const currentEvent = global.event;
71+
global.event = event;
72+
7073
try {
7174
listener(event);
7275
} catch (error) {
@@ -77,6 +80,8 @@ export function executeDispatch(event, listener, inst) {
7780
// TODO: Make sure this error gets logged somehow.
7881
}
7982
}
83+
84+
global.event = currentEvent;
8085
event.currentTarget = null;
8186
}
8287

0 commit comments

Comments
 (0)