Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React prop name collision between 3rd-party libraries

How can I can prevent errors between 3rd-party libraries when they use the same prop name but expect different data types for that prop?

Example: Material-UI's FormControl component expects the 'error' prop to be a boolean, but I'm using Yup (and Formik) for form validation, which sends a string also labeled 'error'. React is saying this in the console:

"Failed prop type: Invalid prop error of type string supplied to FormControl, expected boolean"

My app works despite the error, but I'd like to get rid of it if possible.

like image 602
Matthew Masurka Avatar asked Mar 24 '26 03:03

Matthew Masurka


1 Answers

You can parse your error string to bool value using !!

<FormControl {...props} error={!!yourError} />

What is the !! (not not) operator in JavaScript? for more detail.

like image 97
Sy Tran Avatar answered Mar 25 '26 16:03

Sy Tran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!