Sometimes, we may encounter the ‘ Failed form propType: You provided a checked prop to a form field without an onChange handler.’ warning when we’re developing React components.
In this article, we’ll look at how to fix the ‘ Failed form propType: You provided a checked prop to a form field without an onChange handler.’ warning when we’re developing React components.
Fix ‘ Failed form propType: You provided a checked prop to a form field without an onChange handler.’ Warning
To fix this warning, we can add the checked
prop with a value if we’re creating a controller component.
For instance, we can write:
<input
type="checkbox"
checked={this.props.checked}
onChange={this.onChange}
/>
If it’s an uncontrolled component, we can write populate the defaultChecked
prop:
<input
type="checkbox"
defaultChecked={this.props.checked}
/>
Conclusion
To fix the ‘ Failed form propType: You provided a checked prop to a form field without an onChange handler.’ warning, we can add the checked
prop with a value if we’re creating a controller component.