React Core & Hooks (407 Qs)•Part 7: Supported Libraries, Styling & Integrations (139 - 151)
What is the difference between Flow and PropTypes?
junior LevelCode ExamplereactSupported Libraries
Flow is a static analysis tool (static checker) which uses a superset of the language, allowing you to add type annotations to all of your code and catch an entire class of bugs at compile time.
PropTypes is a basic type checker (runtime checker) which has been patched onto React. It can't check anything other than the types of the props being passed to a given component. If you want more flexible typechecking for your entire project Flow/TypeScript are appropriate choices.
Practical Code Example
javascript
// Flow: Static compile-time checking before code runs (zero runtime bundle overhead)
// @flow
function calculateDiscount(price: number, discountRate: number): number {
return price * (1 - discountRate);
}
// PropTypes: Runtime prop validation in development mode
import PropTypes from 'prop-types';
function PriceTag({ price }) {
return <span>${price}</span>;
}
PriceTag.propTypes = {
price: PropTypes.number.isRequired,
};