Enforce defaultProps for non-required props with react/require-default-props
masterThe react/require-default-props rule ensures that every prop not marked as required has a corresponding defaultProps definition. This rule works with PropTypes, TypeScript, or Flow definitions.
Using defaultProps is preferred over custom default logic (like default function parameters) because React resolves defaultProps before PropTypes typechecking occurs, ensuring that typechecking applies to your default values.
const HelloWorld = ({ name }) => (
<h1>Hello, {name.first} {name.last}!</h1>
);
HelloWorld.propTypes = {
name: PropTypes.shape({
first: PropTypes.string,
last: PropTypes.string,
})
};
HelloWorld.defaultProps = {
name: 'john'
};