Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Experimental Event API: add event component mount phase callback
  • Loading branch information
trueadm committed Apr 23, 2019
commit b31d26f85cf9c2e9e08bc322ebfab105c78a8bcc
13 changes: 13 additions & 0 deletions packages/react-dom/src/events/DOMEventResponderSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ function createReactEventComponent(
targetEventTypes,
createInitialState,
onEvent,
onMount,
onUnmount,
onOwnershipChange,
) {
const testEventResponder = {
targetEventTypes,
createInitialState,
onEvent,
onMount,
onUnmount,
onOwnershipChange,
};
Expand Down Expand Up @@ -559,12 +561,35 @@ describe('DOMEventResponderSystem', () => {
]);
});

it('the event responder onMount() function should fire', () => {
let onMountFired = 0;

const EventComponent = createReactEventComponent(
[],
undefined,
undefined,
() => {
onMountFired++;
},
);

const Test = () => (
<EventComponent>
<button />
</EventComponent>
);

ReactDOM.render(<Test />, container);
expect(onMountFired).toEqual(1);
});

it('the event responder onUnmount() function should fire', () => {
let onUnmountFired = 0;

const EventComponent = createReactEventComponent(
[],
undefined,
undefined,
(event, context, props, state) => {},
() => {
onUnmountFired++;
Expand All @@ -590,7 +615,8 @@ describe('DOMEventResponderSystem', () => {
() => ({
incrementAmount: 5,
}),
(event, context, props, state) => {},
undefined,
undefined,
(context, props, state) => {
counter += state.incrementAmount;
},
Expand Down Expand Up @@ -621,6 +647,7 @@ describe('DOMEventResponderSystem', () => {
}
},
undefined,
undefined,
() => {
onOwnershipChangeFired++;
},
Expand Down
9 changes: 8 additions & 1 deletion packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import {
unhideTextInstance,
unmountEventComponent,
commitEventTarget,
mountEventComponent,
} from './ReactFiberHostConfig';
import {
captureCommitPhaseError,
Expand Down Expand Up @@ -595,6 +596,7 @@ function commitLifeCycles(
case SuspenseComponent:
case IncompleteClassComponent:
case EventTarget:
case EventComponent:
break;
default: {
invariant(
Expand Down Expand Up @@ -835,7 +837,8 @@ function commitContainer(finishedWork: Fiber) {
case ClassComponent:
case HostComponent:
case HostText:
case EventTarget: {
case EventTarget:
case EventComponent: {
return;
}
case HostRoot:
Expand Down Expand Up @@ -1255,6 +1258,10 @@ function commitWork(current: Fiber | null, finishedWork: Fiber): void {
case IncompleteClassComponent: {
return;
}
case EventComponent: {
mountEventComponent(finishedWork.stateNode);
return;
}
default: {
invariant(
false,
Expand Down
4 changes: 1 addition & 3 deletions packages/react-reconciler/src/ReactFiberCompleteWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ import {
createContainerChildSet,
appendChildToContainerChildSet,
finalizeContainerChildren,
mountEventComponent,
updateEventComponent,
handleEventTarget,
} from './ReactFiberHostConfig';
Expand Down Expand Up @@ -813,13 +812,12 @@ function completeWork(
responderState = responder.createInitialState(newProps);
}
eventComponentInstance = workInProgress.stateNode = {
context: null,
props: newProps,
responder,
rootInstance: rootContainerInstance,
state: responderState,
};
mountEventComponent(eventComponentInstance);
markUpdate(workInProgress);
} else {
// Update the props on the event component state node
eventComponentInstance.props = newProps;
Expand Down
6 changes: 5 additions & 1 deletion packages/shared/ReactTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ export type ReactEventResponder = {
props: null | Object,
state: null | Object,
) => boolean,
onMount: (
context: ReactResponderContext,
props: null | Object,
state: null | Object,
) => void,
onUnmount: (
context: ReactResponderContext,
props: null | Object,
Expand All @@ -107,7 +112,6 @@ export type ReactEventResponder = {
};

export type ReactEventComponentInstance = {|
context: null | Object,
props: null | Object,
responder: ReactEventResponder,
rootInstance: mixed,
Expand Down