-
Notifications
You must be signed in to change notification settings - Fork 23
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
add react props & typescript standard and examples #70
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think another really useful thing to mention would by why we should not use React.FC
or FunctionComponent
to type our components. Together with that also shortly explain PropsWithChildren
.
Listing https://react-typescript-cheatsheet.netlify.app/ might also be helpful, it contains (with explanations) a bunch of TS and React examples. It has helped me out a ton in the past.
@cobidev if you can resolve the conflicts, then we can finally merge this in :) |
Use `React.ReactElement` instead of `JSX.Element` Co-authored-by: Leroy Korterink <[email protected]>
type or wrap your prop type in the `PropsWithChildren` type util. | ||
|
||
```ts | ||
type MonkComponentProps = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we be using the interface
keyword instead of the type
keyword?
type MonkComponentProps = { | |
interface MonkComponentProps { |
// other properties | ||
}; | ||
|
||
function MonkComponent({ children }: PropsWithChildren<MonkComponentProps>): ReactElement { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should add this example. The example above is a more concise way to set a type for the children prop. This also creates an anonymous type that can only be inferred using eg the React.ComponentProps<T>
type helper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really see a problem with this, and would actually say that that is an advantage rather than an issue.
- Each component should decide by itself if it accepts children or not.
- If I reuse a prop type that has
children?: ReactNode
defined, then I might accidentally permitchildren
where I don't want them.
type ChildProps = { children?: ReactNode };
function Child({ children }: ChildProps { /* do stuff */ }
type ParentProps = { /* stuff */ } & ChildProps
function Parent(props: ParentProps { /* do stuff */ }
<Parent>/* oops, I can pass children and TS is ok */</ Parent>
No description provided.