Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

16.13 patch #18344

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
147 changes: 110 additions & 37 deletions packages/react-dom/src/__tests__/ReactCompositeComponent-test.js
Expand Up @@ -493,43 +493,6 @@ describe('ReactCompositeComponent', () => {
ReactDOM.render(<Component prop={123} />, container);
});

it('should warn about `setState` in getChildContext', () => {
const container = document.createElement('div');

let renderPasses = 0;

class Component extends React.Component {
state = {value: 0};

getChildContext() {
if (this.state.value === 0) {
this.setState({value: 1});
}
}

render() {
renderPasses++;
return <div />;
}
}
Component.childContextTypes = {};

let instance;

expect(() => {
instance = ReactDOM.render(<Component />, container);
}).toErrorDev(
'Warning: setState(...): Cannot call setState() inside getChildContext()',
);

expect(renderPasses).toBe(2);
expect(instance.state.value).toBe(1);

// Test deduplication; (no additional warnings are expected).
ReactDOM.unmountComponentAtNode(container);
ReactDOM.render(<Component />, container);
});

it('should cleanup even if render() fatals', () => {
class BadComponent extends React.Component {
render() {
Expand Down Expand Up @@ -1786,4 +1749,114 @@ describe('ReactCompositeComponent', () => {
ReactDOM.render(<Shadow />, container);
expect(container.firstChild.tagName).toBe('DIV');
});

it('should not warn on updating function component from componentWillMount', () => {
let _setState;
function A() {
_setState = React.useState()[1];
return null;
}
class B extends React.Component {
UNSAFE_componentWillMount() {
_setState({});
}
render() {
return null;
}
}
function Parent() {
return (
<div>
<A />
<B />
</div>
);
}
const container = document.createElement('div');
ReactDOM.render(<Parent />, container);
});

it('should not warn on updating function component from componentWillUpdate', () => {
let _setState;
function A() {
_setState = React.useState()[1];
return null;
}
class B extends React.Component {
UNSAFE_componentWillUpdate() {
_setState({});
}
render() {
return null;
}
}
function Parent() {
return (
<div>
<A />
<B />
</div>
);
}
const container = document.createElement('div');
ReactDOM.render(<Parent />, container);
ReactDOM.render(<Parent />, container);
});

it('should not warn on updating function component from componentWillReceiveProps', () => {
let _setState;
function A() {
_setState = React.useState()[1];
return null;
}
class B extends React.Component {
UNSAFE_componentWillReceiveProps() {
_setState({});
}
render() {
return null;
}
}
function Parent() {
return (
<div>
<A />
<B />
</div>
);
}
const container = document.createElement('div');
ReactDOM.render(<Parent />, container);
ReactDOM.render(<Parent />, container);
});

it('should warn on updating function component from render', () => {
let _setState;
function A() {
_setState = React.useState()[1];
return null;
}
class B extends React.Component {
render() {
_setState({});
return null;
}
}
function Parent() {
return (
<div>
<A />
<B />
</div>
);
}
const container = document.createElement('div');
expect(() => {
ReactDOM.render(<Parent />, container);
}).toErrorDev(
'Cannot update a component (`A`) while rendering a different component (`B`)',
);
// Dedupe.
ReactDOM.render(<Parent />, container);
});
});
47 changes: 0 additions & 47 deletions packages/react-dom/src/__tests__/ReactDOMComponent-test.js
Expand Up @@ -1242,53 +1242,6 @@ describe('ReactDOMComponent', () => {
);
});

it('should emit a warning once for a named custom component using shady DOM', () => {
const defaultCreateElement = document.createElement.bind(document);

try {
document.createElement = element => {
const container = defaultCreateElement(element);
container.shadyRoot = {};
return container;
};
class ShadyComponent extends React.Component {
render() {
return <polymer-component />;
}
}
const node = document.createElement('div');
expect(() => ReactDOM.render(<ShadyComponent />, node)).toErrorDev(
'ShadyComponent is using shady DOM. Using shady DOM with React can ' +
'cause things to break subtly.',
);
mountComponent({is: 'custom-shady-div2'});
} finally {
document.createElement = defaultCreateElement;
}
});

it('should emit a warning once for an unnamed custom component using shady DOM', () => {
const defaultCreateElement = document.createElement.bind(document);

try {
document.createElement = element => {
const container = defaultCreateElement(element);
container.shadyRoot = {};
return container;
};

expect(() => mountComponent({is: 'custom-shady-div'})).toErrorDev(
'A component is using shady DOM. Using shady DOM with React can ' +
'cause things to break subtly.',
);

// No additional warnings are expected
mountComponent({is: 'custom-shady-div2'});
} finally {
document.createElement = defaultCreateElement;
}
});

it('should treat menuitem as a void element but still create the closing tag', () => {
// menuitem is not implemented in jsdom, so this triggers the unknown warning error
const container = document.createElement('div');
Expand Down
27 changes: 0 additions & 27 deletions packages/react-dom/src/client/ReactDOMComponent.js
Expand Up @@ -7,8 +7,6 @@
* @flow
*/

// TODO: direct imports like some-package/src/* are bad. Fix me.
import {getCurrentFiberOwnerNameInDevOrNull} from 'react-reconciler/src/ReactCurrentFiber';
import {registrationNameModules} from 'legacy-events/EventPluginRegistry';
import {canUseDOM} from 'shared/ExecutionEnvironment';
import endsWith from 'shared/endsWith';
Expand Down Expand Up @@ -90,7 +88,6 @@ import {
import {legacyListenToEvent} from '../events/DOMLegacyEventPluginSystem';

let didWarnInvalidHydration = false;
let didWarnShadyDOM = false;
let didWarnScriptTags = false;

const DANGEROUSLY_SET_INNER_HTML = 'dangerouslySetInnerHTML';
Expand Down Expand Up @@ -509,18 +506,6 @@ export function setInitialProperties(
const isCustomComponentTag = isCustomComponent(tag, rawProps);
if (__DEV__) {
validatePropertiesInDevelopment(tag, rawProps);
if (
isCustomComponentTag &&
!didWarnShadyDOM &&
(domElement: any).shadyRoot
) {
console.error(
'%s is using shady DOM. Using shady DOM with React can ' +
'cause things to break subtly.',
getCurrentFiberOwnerNameInDevOrNull() || 'A component',
);
didWarnShadyDOM = true;
}
}

// TODO: Make sure that we check isMounted before firing any of these events.
Expand Down Expand Up @@ -906,18 +891,6 @@ export function diffHydratedProperties(
suppressHydrationWarning = rawProps[SUPPRESS_HYDRATION_WARNING] === true;
isCustomComponentTag = isCustomComponent(tag, rawProps);
validatePropertiesInDevelopment(tag, rawProps);
if (
isCustomComponentTag &&
!didWarnShadyDOM &&
(domElement: any).shadyRoot
) {
console.error(
'%s is using shady DOM. Using shady DOM with React can ' +
'cause things to break subtly.',
getCurrentFiberOwnerNameInDevOrNull() || 'A component',
);
didWarnShadyDOM = true;
}
}

// TODO: Make sure that we check isMounted before firing any of these events.
Expand Down
26 changes: 26 additions & 0 deletions packages/react-noop-renderer/src/createReactNoop.js
Expand Up @@ -785,6 +785,32 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
};
},

createLegacyRoot() {
const container = {
rootID: '' + idCounter++,
pendingChildren: [],
children: [],
};
const fiberRoot = NoopRenderer.createContainer(
container,
LegacyRoot,
false,
null,
);
return {
_Scheduler: Scheduler,
render(children: ReactNodeList) {
NoopRenderer.updateContainer(children, fiberRoot, null, null);
},
getChildren() {
return getChildren(container);
},
getChildrenAsJSX() {
return getChildrenAsJSX(container);
},
};
},

getChildrenAsJSX(rootID: string = DEFAULT_ROOT_ID) {
const container = rootContainers.get(rootID);
return getChildrenAsJSX(container);
Expand Down
12 changes: 5 additions & 7 deletions packages/react-reconciler/src/ReactCurrentFiber.js
Expand Up @@ -23,8 +23,6 @@ import getComponentName from 'shared/getComponentName';

const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;

type LifeCyclePhase = 'render' | 'getChildContext';

function describeFiber(fiber: Fiber): string {
switch (fiber.tag) {
case HostRoot:
Expand Down Expand Up @@ -57,7 +55,7 @@ export function getStackByFiberInDevAndProd(workInProgress: Fiber): string {
}

export let current: Fiber | null = null;
export let phase: LifeCyclePhase | null = null;
export let isRendering: boolean = false;

export function getCurrentFiberOwnerNameInDevOrNull(): string | null {
if (__DEV__) {
Expand Down Expand Up @@ -88,20 +86,20 @@ export function resetCurrentFiber() {
if (__DEV__) {
ReactDebugCurrentFrame.getCurrentStack = null;
current = null;
phase = null;
isRendering = false;
}
}

export function setCurrentFiber(fiber: Fiber) {
if (__DEV__) {
ReactDebugCurrentFrame.getCurrentStack = getCurrentFiberStackInDev;
current = fiber;
phase = null;
isRendering = false;
}
}

export function setCurrentPhase(lifeCyclePhase: LifeCyclePhase | null) {
export function setIsRendering(rendering: boolean) {
if (__DEV__) {
phase = lifeCyclePhase;
isRendering = rendering;
}
}