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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
4. [Formatting](#formatting)
5. [Comments](#comments)
6. [TypeScript](#typescript)
1. [React props & TypeScript](#react-props-typescript)
7. [GIT](#git)
1. [Branches](#branches)
2. [Commit messages](#commit-messages)
Expand Down Expand Up @@ -355,6 +356,38 @@ writable.

Always prefer `ReadonlyArray` over a regular `Array` unless it must be possible to modify the Array.

### React props & TypeScript

`interface` is preferred over `type` when defining the component props. Are a little more powerful
cobiwave marked this conversation as resolved.
Show resolved Hide resolved
than the corresponding type declaration, but for a set of react props, it likely doesn't matter.
psimk marked this conversation as resolved.
Show resolved Hide resolved

Also it is not necessary to type the `returnType` because it is inferred from what you actually return, so
can be omitted most of the time.
cobiwave marked this conversation as resolved.
Show resolved Hide resolved

So the best approach and example would be something like this:

```
cobiwave marked this conversation as resolved.
Show resolved Hide resolved
interface FancyButtonProps {
color: string
cobiwave marked this conversation as resolved.
Show resolved Hide resolved
}

const FancyButton = (props: FancyButtonProps) => {
cobiwave marked this conversation as resolved.
Show resolved Hide resolved
...
}
```

or if you're using props destructuring:
cobiwave marked this conversation as resolved.
Show resolved Hide resolved

```
psimk marked this conversation as resolved.
Show resolved Hide resolved
interface FancyButtonProps {
color: string
cobiwave marked this conversation as resolved.
Show resolved Hide resolved
}

const FancyButton = ({ color }: FancyButtonProps) => {
...
cobiwave marked this conversation as resolved.
Show resolved Hide resolved
}
```

## GIT

### Branches
Expand Down