with-default-props
lets you write DefaultProps
for function components in a painless way.
API
Only one function is exported. Properties appearing in defaultProps
will become optional in WrappedComponent.
- NPM:
npm install with-default-props
- Yarn:
yarn add with-default-props
Example
import withDefaultProps from 'with-default-props' type Props = text: string; void;; { return <div =>propstext</div>;} // `onClick` is optional now.const Wrapped = { // ✅ return <Wrapped ="hello"></Wrapped>} { // ✅ return <Wrapped ="hello" =></Wrapped>} { // ❌ // Error: "text" is missing! return <Wrapped =></Wrapped>}
Alernative
React functional component default props vs default parameters, but they don't play well with TypeScript.