Skip to content

Another way of defining default Props #415

Closed

Description

@ankitchouhan1020
const GreetComponent = ({ name, age, status }: RequiredProps & DefaultProps) => (
  <div>{`Hello, my name is ${name}, ${age}, ${typeof status === string ? status : status[0]}`}</div>
);

const defaultProps = {
  age: 25,
  status: "" // just for example
} as DefaultProps;

GreetComponent.defaultProps = defaultProps;

type RequiredProps = {
  name: string;
}

type DefaultProps = {
 age: number,
 status: string | string[]
}

In some cases, we might want to have multiple types in a default prop. This option gives the flexibility to assign both types.
Also, it checks for defaultProp type validation.

Activity

  1. ankitchouhan1020 commented on May 14, 2021

    @ankitchouhan1020
    Author

    Yes, but here we are defining DefaultProps explicitly. Not an issue, just another way of doing this.

  2. swyxio commented on May 14, 2021

    @swyxio
    Collaborator

    i'll add a small note :) thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions