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

New Rule: onChange or readOnly prop when checked is set on an input element #3143

Closed
karlhorky opened this issue Nov 17, 2021 · 1 comment · Fixed by #3680
Closed

New Rule: onChange or readOnly prop when checked is set on an input element #3143

karlhorky opened this issue Nov 17, 2021 · 1 comment · Fixed by #3680

Comments

@karlhorky
Copy link
Contributor

karlhorky commented Nov 17, 2021

Hi there! 👋

The following code produces a warning from React (see below):

<input
  type="checkbox"
  checked={marketingConsent}
  onClick={(event) => {
    setMarketingConsent(event.currentTarget.checked);
  }}
/>

This is the warning:

Warning: You provided a `checked` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultChecked`. Otherwise, set either `onChange` or `readOnly`.

Since React shows a warning for it, maybe there can be a lint rule for it too? To guide developers towards either:

  • usage of onChange or readOnly props with checked prop on an input
  • usage of defaultChecked for uncontrolled components
@ljharb
Copy link
Member

ljharb commented Nov 19, 2021

Sure, that seems reasonable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants