Skip to content

Fix: Ignore Symbol/Function in input defaultValue (fixes #27896) #33942

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-dom-bindings/src/client/ToStringValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ export function getToStringValue(value: mixed): ToStringValue {
// function, symbol are assigned as empty strings
return '';
}
}
}
28 changes: 28 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3109,4 +3109,32 @@ describe('ReactDOMInput', () => {
expect(log).toEqual(['']);
expect(node.value).toBe('a');
});
// Remove your custom warning tests (they're not needed since React already warns)
// Keep the existing Symbol/Function value tests but update the assertions:

it('treats initial Symbol value as an empty string', async () => {
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<input value={Symbol('test')} />);
});
assertConsoleErrorDev([
'Invalid value for prop `value` on <input> tag. Either remove it from the element, or pass a string or number value to keep it in the DOM. For details, see https://react.dev/link/attribute-behavior \n in input (at **)',
'You provided a `value` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultValue`. Otherwise, set either `onChange` or `readOnly`.\n in input (at **)',
]);
expect(container.firstChild.value).toBe('');
});

it('treats initial function value as an empty string', async () => {
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<input value={() => {}} />);
});
assertConsoleErrorDev([
'Invalid value for prop `value` on <input> tag. Either remove it from the element, or pass a string or number value to keep it in the DOM. For details, see https://react.dev/link/attribute-behavior \n in input (at **)',
'You provided a `value` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultValue`. Otherwise, set either `onChange` or `readOnly`.\n in input (at **)',
]);
expect(container.firstChild.value).toBe('');
});
});