Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another way of defining default Props #415

Closed
ankitchouhan1020 opened this issue May 13, 2021 · 3 comments
Closed

Another way of defining default Props #415

ankitchouhan1020 opened this issue May 13, 2021 · 3 comments

Comments

@ankitchouhan1020
Copy link

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.

@swyxio
Copy link
Collaborator

swyxio commented May 13, 2021

@ankitchouhan1020
Copy link
Author

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

@swyxio
Copy link
Collaborator

swyxio commented May 14, 2021

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants