From d3af2f2a5d7eee1cefb403eee7e67a4f292f5ca8 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Wed, 24 Apr 2019 10:41:24 +0100 Subject: [PATCH] Experimental Event API: add event component mount phase callback (#15480) --- .../src/events/DOMEventResponderSystem.js | 18 +++++++++- .../DOMEventResponderSystem-test.internal.js | 33 ++++++++++++++++++- .../src/ReactFiberCommitWork.js | 9 ++++- .../src/ReactFiberCompleteWork.js | 4 +-- packages/shared/ReactTypes.js | 10 ++++-- 5 files changed, 65 insertions(+), 9 deletions(-) diff --git a/packages/react-dom/src/events/DOMEventResponderSystem.js b/packages/react-dom/src/events/DOMEventResponderSystem.js index 2e3da99145f45..cc45200622cc6 100644 --- a/packages/react-dom/src/events/DOMEventResponderSystem.js +++ b/packages/react-dom/src/events/DOMEventResponderSystem.js @@ -612,7 +612,10 @@ function triggerOwnershipListeners(): void { const instance = listeningInstances[i]; const {props, responder, state} = instance; currentInstance = instance; - responder.onOwnershipChange(eventResponderContext, props, state); + const onOwnershipChange = responder.onOwnershipChange; + if (onOwnershipChange !== undefined) { + onOwnershipChange(eventResponderContext, props, state); + } } } finally { currentInstance = previousInstance; @@ -626,6 +629,19 @@ export function mountEventResponder( if (responder.onOwnershipChange !== undefined) { ownershipChangeListeners.add(eventComponentInstance); } + const onMount = responder.onMount; + if (onMount !== undefined) { + let {props, state} = eventComponentInstance; + currentEventQueue = createEventQueue(); + currentInstance = eventComponentInstance; + try { + onMount(eventResponderContext, props, state); + } finally { + currentEventQueue = null; + currentInstance = null; + currentTimers = null; + } + } } export function unmountEventResponder( diff --git a/packages/react-dom/src/events/__tests__/DOMEventResponderSystem-test.internal.js b/packages/react-dom/src/events/__tests__/DOMEventResponderSystem-test.internal.js index 8d476b4568cd5..a92223e39078f 100644 --- a/packages/react-dom/src/events/__tests__/DOMEventResponderSystem-test.internal.js +++ b/packages/react-dom/src/events/__tests__/DOMEventResponderSystem-test.internal.js @@ -21,6 +21,7 @@ function createReactEventComponent( onEvent, onEventCapture, onRootEvent, + onMount, onUnmount, onOwnershipChange, stopLocalPropagation, @@ -32,6 +33,7 @@ function createReactEventComponent( onEvent, onEventCapture, onRootEvent, + onMount, onUnmount, onOwnershipChange, stopLocalPropagation: stopLocalPropagation || false, @@ -395,6 +397,7 @@ describe('DOMEventResponderSystem', () => { undefined, undefined, undefined, + undefined, true, ); @@ -554,6 +557,31 @@ describe('DOMEventResponderSystem', () => { ]); }); + it('the event responder onMount() function should fire', () => { + let onMountFired = 0; + + const EventComponent = createReactEventComponent( + [], + undefined, + undefined, + undefined, + undefined, + undefined, + () => { + onMountFired++; + }, + ); + + const Test = () => ( + +