You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constGreetComponent=({ name, age, status }: RequiredProps&DefaultProps)=>(<div>{`Hello, my name is ${name}, ${age}, ${typeofstatus===string ? status : status[0]}`}</div>);constdefaultProps={age: 25,status: ""// just for example}asDefaultProps;GreetComponent.defaultProps=defaultProps;typeRequiredProps={name: string;}typeDefaultProps={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
swyxio commentedon May 13, 2021
like this? https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/default_props/#typing-defaultprops
ankitchouhan1020 commentedon May 14, 2021
Yes, but here we are defining DefaultProps explicitly. Not an issue, just another way of doing this.
swyxio commentedon May 14, 2021
i'll add a small note :) thank you!