Type Checking
ScandiPWA uses PropTypes to help verify that the expected props are passed, and catch bugs
Last updated
Was this helpful?
ScandiPWA uses PropTypes to help verify that the expected props are passed, and catch bugs
Last updated
Was this helpful?
is a library for React that verifies at runtime if props have the correct type, and if required props are passed.
PropTypes are only used in development builds, which means that production builds will be more efficient - but it also means that you should not rely on PropTypes being present in the production build!
PropTypes are declared as a static field:
instanceOf
to expect an instance of a class
oneOf
to expect an enum-like value that can have one of the specified values
oneOfType
to indicate that this prop may have one of the specified types
arrayOf
or objectOf
to specify that this prop is an array or object where each value has the specified type
shape
or exact
to specify that the prop should be an object, as well as what keys and values you expect it to have
For example, you could specify that a prop expecting a payment method should be given an object with two string properties:
However, it is likely that this "complex" type would be needed in multiple places in the application. Copy-pasting the same PropType definition would lead to code duplication, and a hard-to-maintain codebase. Instead, we prefer defining these types in the type
directory and exporting them for re-use:
Now, we can use this type definition anywhere we want:
Sometimes you want to specify that a prop expects something more complex than a number or a string. You can check the to learn how to use: