Per React's documentation on PropTypes:
Note that for performance reasons
propTypes
is only checked in development mode.
However, when I build my app with Webpack, I see the propTypes that I have defined still in the final bundle.
I am setting the NODE_ENV to production. Is this behavior expected?
My theory:
The PropTypes validation is skipped, although the PropType definitions are still in production code. If this theory is correct, are there any best practices for stripping PropTypes in prod code?
'prop-types' should be listed in the project's dependencies, not devDependencies.
In this example, we are using a class component, but the same functionality could also be applied to function components, or components created by React.memo or React.forwardRef . PropTypes exports a range of validators that can be used to make sure the data you receive is valid.
Props and PropTypes are important mechanisms for passing read-only attributes between React components. We can use React props, short for properties, to send data from one component to another. If a component receives the wrong type of props, it can cause bugs and unexpected errors in your app.
Most React apps will have their files “bundled” using tools like Webpack, Rollup or Browserify. Bundling is the process of following imported files and merging them into a single file: a “bundle”. This bundle can then be included on a webpage to load an entire app at once.
Babel plugin that does the trick:
https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With