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

How to handle default props? #4

Closed
rssfrncs opened this issue Jun 2, 2018 · 7 comments
Closed

How to handle default props? #4

rssfrncs opened this issue Jun 2, 2018 · 7 comments

Comments

@rssfrncs
Copy link

rssfrncs commented Jun 2, 2018

Would be great to have a section detailing the different approaches to handling default props.

@swyxio
Copy link
Collaborator

swyxio commented Jun 2, 2018

yeah I get mixed messages from the community on this. I like to assign when destructuring, but others like to use defaultProps. want to put in a PR?

@ghost
Copy link

ghost commented Jun 3, 2018

Putting this as a comment but willing to propose a PR if @rssfrncs is not taking this.

interface IMyComponentProps {
  firstProp: string;
  secondProp: IPerson[];
}

export class MyComponent extends React.Component<IMyComponentProps, {}> {
  static defaultProps: Partial<IMyComponentProps> = {
    firstProp: "default",
  };
}

@tsiq-swyx
Copy link
Contributor

tsiq-swyx commented Jun 3, 2018

I dont use defaultProps myself but just wanted to add that the Spectrum community also suggested adding a withDefaultProps type HOC: https://twitter.com/code_punkt/status/1000105182565490688

also, here is a slightly longer approach also using Omit: https://levelup.gitconnected.com/ultimate-react-component-patterns-with-typescript-2-8-82990c516935

Yours seem much simpler!

would love a PR here, this is the top requested info based on feedback from reddit/hn/twitter/spectrum

@rssfrncs
Copy link
Author

rssfrncs commented Jun 3, 2018

@Attrash-Islam go ahead with a PR buddy!

@ghost
Copy link

ghost commented Jun 3, 2018

@rssfrncs Thanks! I will do it tomorrow after reviewing the whole repository so it match the README with the other section in the same format 😉

@rssfrncs
Copy link
Author

rssfrncs commented Jun 4, 2018

Have you checked out this similar repo? It has an interesting approach to default props. Whilst it is focused on Redux React apps there is a lot of overlap.

@ghost
Copy link

ghost commented Jun 4, 2018

The way they do it is not that good since you need to update your DefaultProps interface every time you want to include additional prop (see how initialCount: number is duplicated).

My suggestion is using Partial type feature which gives you to extends the defaultProps whenever you want without any additional type extensions

tsiq-swyx added a commit that referenced this issue Jun 4, 2018
[defaultProps] - Related to "How to handle default props?" #4
peterjohnson4987 added a commit to peterjohnson4987/full-stack-developer-react that referenced this issue Oct 3, 2022
aleksandaralek added a commit to aleksandaralek/typescript-react-cheatsheet that referenced this issue Oct 24, 2023
atsumi000105 added a commit to atsumi000105/typescript-cheatsheets-react that referenced this issue Dec 8, 2023
secretsuperstar1109 added a commit to secretsuperstar1109/react-typescript-cheatsheets that referenced this issue Dec 9, 2023
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

3 participants