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

add react props & typescript standard and examples #70

Merged
merged 12 commits into from
Feb 21, 2022
Merged

add react props & typescript standard and examples #70

merged 12 commits into from
Feb 21, 2022

Conversation

cobiwave
Copy link
Contributor

@cobiwave cobiwave commented Mar 8, 2021

No description provided.

@cobiwave cobiwave changed the title add react props & typescript standar and examples add react props & typescript standard and examples Mar 8, 2021
Copy link

@psimk psimk left a 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.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@cobiwave cobiwave requested review from ThaNarie and ThijsTyZ March 17, 2021 21:01
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@cobiwave cobiwave requested review from ThijsTyZ and psimk April 19, 2021 00:43
@psimk
Copy link

psimk commented Oct 11, 2021

@cobidev if you can resolve the conflicts, then we can finally merge this in :)

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
Use `React.ReactElement` instead of `JSX.Element`

Co-authored-by: Leroy Korterink <[email protected]>
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@psimk psimk requested review from leroykorterink, ThaNarie and ThijsTyZ and removed request for Miguel-Bento-Github January 18, 2022 10:34
type or wrap your prop type in the `PropsWithChildren` type util.

```ts
type MonkComponentProps = {
Copy link
Collaborator

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?

Suggested change
type MonkComponentProps = {
interface MonkComponentProps {

// other properties
};

function MonkComponent({ children }: PropsWithChildren<MonkComponentProps>): ReactElement {
Copy link
Collaborator

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.

Copy link

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.

  1. Each component should decide by itself if it accepts children or not.
  2. If I reuse a prop type that has children?: ReactNode defined, then I might accidentally permit children 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>

@psimk psimk merged commit f2243fe into mediamonks:main Feb 21, 2022
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

Successfully merging this pull request may close these issues.

6 participants