As of React v15.5 PropTypes are deprecated in the React package and have been given a package of their own.
Change is an inevitable part of life.
The upshot is twofold. 1) If you use another type-checking mechanism, you don’t have to worry about shipping unnecessary bulk to your end users. Omit the import and bank the weight loss. 2) Your PropType declarations are going to be more svelte than you ever thought possible.
The downside is that if you’ve been ignoring deprecation warnings about calling PropTypes directly since v15.3 (no judgement), that chicken has come home to roost.
Let’s get into it, but first you’ll need to install the package in your project.
Or, you know, do the Yarn thing. Either way here’s your old simple component with validation:
And here it is the new way:
Actually, scratch that. We promised svelte; so give it a destructured spin like so:
That, dear friends, is pretty as a picture, and the good news is it all works precisely like you’re used to. Warnings in development. Silence in production.
The only notable exception is the one we mentioned above: directly invoked PropTypes. Those checks will automatically call the following block in production:
Throwing an error and breaking stuff. You’ll have to refactor those invocations in your code or work out your own fork of prop-types
to keep them on the upgrade train.
Whatever you do, don’t sweat this change. It’s a net gain with a mild inconvenience tax.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!