Skip to content

Commit addffb8

Browse files
committed
Avoid precomputing stringable values
1 parent cc63851 commit addffb8

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

packages/react-dom/src/client/ReactDOMInput.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -243,35 +243,36 @@ export function postMountWrapper(
243243
// Do not assign value if it is already set. This prevents user text input
244244
// from being lost during SSR hydration.
245245
if (props.hasOwnProperty('value') || props.hasOwnProperty('defaultValue')) {
246-
const initialValue = node._wrapperState.initialValue;
247-
const currentValue = node.value;
248-
const value = disableInputAttributeSyncing
249-
? getToStringValue(props.value)
250-
: initialValue;
251246
const type = props.type;
247+
const isButton = type === 'submit' || type === 'reset';
252248

253249
// Avoid setting value attribute on submit/reset inputs as it overrides the
254250
// default value provided by the browser. See: #12872
255-
if (type === 'submit' || type === 'reset') {
256-
if (props.value !== undefined && props.value !== null) {
257-
node.setAttribute('value', toString(value));
258-
}
259-
251+
if (isButton && (props.value === undefined || props.value === null)) {
260252
return;
261253
}
262254

255+
const initialValue = node._wrapperState.initialValue;
256+
263257
// Do not assign value if it is already set. This prevents user text input
264258
// from being lost during SSR hydration.
265259
if (!isHydrating) {
266-
// Do not re-assign the value property if is empty. This
267-
// potentially avoids a DOM write and prevents Firefox (~60.0.1) from
268-
// prematurely marking required inputs as invalid
269260
if (disableInputAttributeSyncing) {
270261
// When not syncing the value attribute, the value property points
271262
// directly to the React prop. Only assign it if it exists.
272263
if (props.hasOwnProperty('value')) {
273-
if (value !== currentValue) {
274-
node.value = toString(value);
264+
const value = toString(getToStringValue(props.value));
265+
266+
// Always assign on buttons so that it is possible to assign an
267+
// empty string to clear button text.
268+
//
269+
// Otherwise, do not re-assign the value property if is empty. This
270+
// potentially avoids a DOM write and prevents Firefox (~60.0.1) from
271+
// prematurely marking required inputs as invalid. Equality is compared
272+
// to the current value in case the browser provided value is not an
273+
// empty string.
274+
if (isButton || value !== node.value) {
275+
node.value = value;
275276
}
276277
}
277278
} else {
@@ -281,8 +282,8 @@ export function postMountWrapper(
281282
// 1. The value React property when present
282283
// 2. The defaultValue React property when present
283284
// 3. An empty string
284-
if (value !== currentValue) {
285-
node.value = toString(value);
285+
if (initialValue !== node.value) {
286+
node.value = toString(initialValue);
286287
}
287288
}
288289
}

0 commit comments

Comments
 (0)