Skip to content

Commit a112448

Browse files
[DOM] Treat omitted Fragment Event listener options same as capture: false (#37251)
`FragmentInstance` tracks its event listeners so they can be applied to children added later, and matches them by a normalized options identity. Omitted options currently normalize to a different identity than an explicit `false` or `{capture: false}`, even though both mean `capture: false` per the `EventTarget` contract, where listener identity is the tuple of type, callback, and capture flag. As a result, a listener added without an options argument cannot be removed with an explicit capture-false value (or the reverse). This change normalizes omitted options to the same capture-false identity as `false` and `{capture: false}`. The first commit adds a test to the FragmentRef suite characterizing the current behavior; the second commit contains the fix and the updated assertions. --------- Co-authored-by: Claude Code (kimi-k3[1m]) <[email protected]>
1 parent 3c397fe commit a112448

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3152,7 +3152,7 @@ function normalizeListenerOptions(
31523152
opts: ?EventListenerOptionsOrUseCapture,
31533153
): string {
31543154
if (opts == null) {
3155-
return '0';
3155+
return 'c=0';
31563156
}
31573157

31583158
if (typeof opts === 'boolean') {

‎packages/react-dom/src/__tests__/ReactDOMFragmentRefs-test.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,47 @@ describe('FragmentRefs', () => {
832832
expect(logs).toEqual([]);
833833
});
834834

835+
// @gate enableFragmentRefs
836+
it('matches listeners by their normalized capture flag', async () => {
837+
const fragmentRef = React.createRef();
838+
const childRef = React.createRef();
839+
const root = ReactDOMClient.createRoot(container);
840+
const logs = [];
841+
842+
function addedWithOmittedOptions() {
843+
logs.push('addedWithOmittedOptions');
844+
}
845+
846+
function addedWithCaptureFalse() {
847+
logs.push('addedWithCaptureFalse');
848+
}
849+
850+
await act(() => {
851+
root.render(
852+
<Fragment ref={fragmentRef}>
853+
<div ref={childRef}>child</div>
854+
</Fragment>,
855+
);
856+
});
857+
858+
fragmentRef.current.addEventListener('click', addedWithOmittedOptions);
859+
fragmentRef.current.addEventListener('click', addedWithCaptureFalse, {
860+
capture: false,
861+
});
862+
863+
// Omitted options and an explicit capture: false are the same
864+
// EventTarget listener identity, so each removal should match.
865+
fragmentRef.current.removeEventListener(
866+
'click',
867+
addedWithOmittedOptions,
868+
false,
869+
);
870+
fragmentRef.current.removeEventListener('click', addedWithCaptureFalse);
871+
872+
childRef.current.click();
873+
expect(logs).toEqual([]);
874+
});
875+
835876
// @gate enableFragmentRefs
836877
it('adds and removes event listeners from children with multiple fragments', async () => {
837878
const fragmentRef = React.createRef();

0 commit comments

Comments
 (0)