with-default-props
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

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.

function withDefaultProps(Component, defaultProps): WrappedComponent

  • NPM: npm install with-default-props
  • Yarn: yarn add with-default-props

Example

import { withDefaultProps } from 'with-default-props'
 
type Props = {
    text: string;
    onClick: () => void;
};
 
function Component(props: Props) {
    return <div onClick={props.onClick}>{props.text}</div>;
}
 
// `onClick` is optional now.
const Wrapped = withDefaultProps(Component, { onClick: () => {} })
 
 
function App1() {
    // ✅
    return <Wrapped text="hello"></Wrapped>
}
 
function App2() {
    // ✅
    return <Wrapped text="hello" onClick={() => {}}></Wrapped>
}
 
function App3() {
    // ❌
    // Error: "text" is missing!
    return <Wrapped onClick={() => {}}></Wrapped>
}

Alernative

React functional component default props vs default parameters, but they don't play well with TypeScript.

Package Sidebar

Install

npm i with-default-props

Weekly Downloads

146

Version

0.2.1

License

MIT

Unpacked Size

12.9 kB

Total Files

11

Last publish

Collaborators

  • zzzen