mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Warn on nested EventTragets in experimental event API (#15287)
This commit is contained in:
@@ -190,6 +190,11 @@ export function getChildHostContextForEvent(
|
||||
let eventData = null;
|
||||
|
||||
if (type === REACT_EVENT_COMPONENT_TYPE) {
|
||||
warning(
|
||||
parentHostContextDev.eventData === null ||
|
||||
!parentHostContextDev.eventData.isEventTarget,
|
||||
'validateDOMNesting: React event targets must not have event components as children.',
|
||||
);
|
||||
eventData = {
|
||||
isEventComponent: true,
|
||||
isEventTarget: false,
|
||||
|
||||
@@ -268,6 +268,10 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
|
||||
) {
|
||||
if (__DEV__ && enableEventAPI) {
|
||||
if (type === REACT_EVENT_COMPONENT_TYPE) {
|
||||
warning(
|
||||
parentHostContext !== EVENT_TARGET_CONTEXT,
|
||||
'validateDOMNesting: React event targets must not have event components as children.',
|
||||
);
|
||||
return EVENT_COMPONENT_CONTEXT;
|
||||
} else if (type === REACT_EVENT_TARGET_TYPE) {
|
||||
warning(
|
||||
|
||||
@@ -200,6 +200,25 @@ describe('ReactFiberEvents', () => {
|
||||
'Warning: validateDOMNesting: React event targets must be direct children of event components.',
|
||||
);
|
||||
});
|
||||
|
||||
it('should warn if an event target has an event component as a child', () => {
|
||||
const Test = () => (
|
||||
<EventComponent>
|
||||
<EventTarget>
|
||||
<EventComponent>
|
||||
<span>Child 1</span>
|
||||
</EventComponent>
|
||||
</EventTarget>
|
||||
</EventComponent>
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
ReactNoop.render(<Test />);
|
||||
expect(Scheduler).toFlushWithoutYielding();
|
||||
}).toWarnDev(
|
||||
'Warning: validateDOMNesting: React event targets must not have event components as children.',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('TestRenderer', () => {
|
||||
@@ -368,6 +387,26 @@ describe('ReactFiberEvents', () => {
|
||||
'Warning: validateDOMNesting: React event targets must be direct children of event components.',
|
||||
);
|
||||
});
|
||||
|
||||
it('should warn if an event target has an event component as a child', () => {
|
||||
const Test = () => (
|
||||
<EventComponent>
|
||||
<EventTarget>
|
||||
<EventComponent>
|
||||
<span>Child 1</span>
|
||||
</EventComponent>
|
||||
</EventTarget>
|
||||
</EventComponent>
|
||||
);
|
||||
|
||||
const root = ReactTestRenderer.create(null);
|
||||
expect(() => {
|
||||
root.update(<Test />);
|
||||
expect(Scheduler).toFlushWithoutYielding();
|
||||
}).toWarnDev(
|
||||
'Warning: validateDOMNesting: React event targets must not have event components as children.',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ReactDOM', () => {
|
||||
@@ -535,6 +574,26 @@ describe('ReactFiberEvents', () => {
|
||||
'Warning: validateDOMNesting: React event targets must be direct children of event components.',
|
||||
);
|
||||
});
|
||||
|
||||
it('should warn if an event target has an event component as a child', () => {
|
||||
const Test = () => (
|
||||
<EventComponent>
|
||||
<EventTarget>
|
||||
<EventComponent>
|
||||
<span>Child 1</span>
|
||||
</EventComponent>
|
||||
</EventTarget>
|
||||
</EventComponent>
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
const container = document.createElement('div');
|
||||
ReactDOM.render(<Test />, container);
|
||||
expect(Scheduler).toFlushWithoutYielding();
|
||||
}).toWarnDev(
|
||||
'Warning: validateDOMNesting: React event targets must not have event components as children.',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ReactDOMServer', () => {
|
||||
|
||||
@@ -133,6 +133,10 @@ export function getChildHostContextForEvent(
|
||||
): HostContext {
|
||||
if (__DEV__ && enableEventAPI) {
|
||||
if (type === REACT_EVENT_COMPONENT_TYPE) {
|
||||
warning(
|
||||
parentHostContext !== EVENT_TARGET_CONTEXT,
|
||||
'validateDOMNesting: React event targets must not have event components as children.',
|
||||
);
|
||||
return EVENT_COMPONENT_CONTEXT;
|
||||
} else if (type === REACT_EVENT_TARGET_TYPE) {
|
||||
warning(
|
||||
|
||||
Reference in New Issue
Block a user