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

1.0.4 • Public • Published

WithDefaultProps for Fender React UI Kit

Description

Motivation: React Class Component type definitions don't support type checking on default props, but functional components do (weird).

This adds a function component wrapped to the class and inject default props there so we can get accurate intellisense and autocompletion features, and a happy compiler that doesn't complain about props that might be undefined

Installation and Usage

yarn add @fenderdigital/react-with-default-props --save
import { withDefaultProps, ComponentWithDefaults, DefaultProps } from '@fenderdigital/react-with-default-props';
// also available is PureComponentWithDefaults

interface PropsInterface {
  requiredString: string;
  optionalString?: string;
}

class MyComponent extends ComponentWithDefaults<PropsInterface> {
  public static defaultProps: DefaultProps<PropsInterface> = {
    optionalString: 'optional',
  }

  render() {
    return <div>{this.props.optionalString}</div>;
  }
}

export default withDefaultProps<PropsInterface>(MyComponent, MyComponent.defaultProps);

// you can also optionally do it like so:

class MyComponent extends ComponentWithDefaults<PropsInterface> {
  render() {
    return <div>{this.props.optionalString}</div>;
  }
}

export default withDefaultProps<PropsInterface>(MyComponent, {
  optionalString: 'optional',
});


// Function Component Example

import { FCWithDefaults, DefaultProps } from '@fenderdigital/react-with-default-props';

interface PropsInterface {
  requiredString: string;
  optionalString?: string;
}

const defaultProps: DefaultProps = {
  optionalString: 'this is optional',
}

const MyComponent: FCWithDefaults<PropsInterface> = props => {
  return <div>{this.props.optionalString}</div>;
}

export default withDefaultProps(MyComponent, defaultProps);

Development

Check the Monorepo README for development documentation.

/@fenderdigital/react-with-default-props/

    Package Sidebar

    Install

    npm i @fenderdigital/react-with-default-props

    Weekly Downloads

    2

    Version

    1.0.4

    License

    UNLICENSED

    Unpacked Size

    55.7 kB

    Total Files

    12

    Last publish

    Collaborators

    • stevenhuynh
    • markv242
    • coopercodecomposer
    • sherbert
    • jcuanan
    • derduher
    • philipyun
    • theloopla
    • jemnon