From 71a692f257caca531fd2a3cb5be9ef2fe755d4c4 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Mon, 20 Mar 2023 23:18:30 -0400 Subject: [PATCH 1/5] Extract setting properties into a helper whether it's initial or update --- .../src/client/ReactDOMComponent.js | 467 ++++++------------ 1 file changed, 158 insertions(+), 309 deletions(-) diff --git a/packages/react-dom-bindings/src/client/ReactDOMComponent.js b/packages/react-dom-bindings/src/client/ReactDOMComponent.js index ecb08dda7d358..db56aad9af3fd 100644 --- a/packages/react-dom-bindings/src/client/ReactDOMComponent.js +++ b/packages/react-dom-bindings/src/client/ReactDOMComponent.js @@ -287,162 +287,121 @@ export function trapClickOnNonInteractiveElement(node: HTMLElement) { node.onclick = noop; } -function setInitialDOMProperties( - tag: string, +function setProp( domElement: Element, - nextProps: Object, + tag: string, + key: string, + value: mixed, isCustomComponentTag: boolean, + rawProps: any, ): void { - for (const propKey in nextProps) { - if (!nextProps.hasOwnProperty(propKey)) { - continue; + switch (key) { + case 'style': { + if (value != null && typeof value !== 'object') { + throw new Error( + 'The `style` prop expects a mapping from style properties to values, ' + + "not a string. For example, style={{marginRight: spacing + 'em'}} when " + + 'using JSX.', + ); + } + if (__DEV__) { + if (value) { + // Freeze the next style object so that we can assume it won't be + // mutated. We have already warned for this in the past. + Object.freeze(value); + } + } + // Relies on `updateStylesByID` not mutating `styleUpdates`. + setValueForStyles(domElement, value); + break; } - const nextProp = nextProps[propKey]; - switch (propKey) { - case 'style': { - if (nextProp != null && typeof nextProp !== 'object') { + case 'dangerouslySetInnerHTML': { + if (value != null) { + if (typeof value !== 'object' || !('__html' in value)) { throw new Error( - 'The `style` prop expects a mapping from style properties to values, ' + - "not a string. For example, style={{marginRight: spacing + 'em'}} when " + - 'using JSX.', + '`props.dangerouslySetInnerHTML` must be in the form `{__html: ...}`. ' + + 'Please visit https://reactjs.org/link/dangerously-set-inner-html ' + + 'for more information.', ); } - if (__DEV__) { - if (nextProp) { - // Freeze the next style object so that we can assume it won't be - // mutated. We have already warned for this in the past. - Object.freeze(nextProp); - } - } - // Relies on `updateStylesByID` not mutating `styleUpdates`. - setValueForStyles(domElement, nextProp); - break; - } - case 'dangerouslySetInnerHTML': { - if (nextProp != null) { - if (typeof nextProp !== 'object' || !('__html' in nextProp)) { + const nextHtml: any = value.__html; + if (nextHtml != null) { + if (rawProps.children != null) { throw new Error( - '`props.dangerouslySetInnerHTML` must be in the form `{__html: ...}`. ' + - 'Please visit https://reactjs.org/link/dangerously-set-inner-html ' + - 'for more information.', + 'Can only set one of `children` or `props.dangerouslySetInnerHTML`.', ); } - const nextHtml = nextProp.__html; - if (nextHtml != null) { - if (nextProps.children != null) { - throw new Error( - 'Can only set one of `children` or `props.dangerouslySetInnerHTML`.', - ); - } - if (disableIEWorkarounds) { - domElement.innerHTML = nextHtml; - } else { - setInnerHTML(domElement, nextHtml); - } + if (disableIEWorkarounds) { + domElement.innerHTML = nextHtml; + } else { + setInnerHTML(domElement, nextHtml); } } - break; } - case 'children': { - if (typeof nextProp === 'string') { - // Avoid setting initial textContent when the text is empty. In IE11 setting - // textContent on a