Type Checking
ScandiPWA uses PropTypes to help verify that the expected props are passed, and catch bugs
PropTypes 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:
Advanced Types
Sometimes you want to specify that a prop expects something more complex than a number or a string. You can check the official documentation to learn how to use:
instanceOf
to expect an instance of a classoneOf
to expect an enum-like value that can have one of the specified valuesoneOfType
to indicate that this prop may have one of the specified typesarrayOf
orobjectOf
to specify that this prop is an array or object where each value has the specified typeshape
orexact
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:
Last updated