Skip to content

Commit 078dd27

Browse files
authored
add some structure, recognize contributors inline for now
1 parent f7615cc commit 078dd27

File tree

1 file changed

+78
-100
lines changed

1 file changed

+78
-100
lines changed

README.md

Lines changed: 78 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,30 @@
55

66
<summary><b>Table of Contents</b></summary>
77

8-
- [Prerequisites](#prerequisites)
9-
- [React + Typescript Starter Kits](#react---typescript-starter-kits)
10-
- [Import React](#import-react)
11-
- [Stateless Functional Components](#stateless-functional-components)
12-
- [Stateful Class-based Components](#stateful-class-based-components)
13-
- [Typing DefaultProps](#typing-defaultprops)
14-
- [Extracting Prop Types](#extracting-prop-types)
15-
- [Basic Prop Types Examples](#basic-prop-types-examples)
16-
- [Useful React Type Examples](#useful-react-type-examples)
17-
- [Forms and Events](#forms-and-events)
18-
- [Higher Order Components/Render Props](#higher-order-components-render-props)
19-
- [Context](#context)
20-
- [References/createRef](#references-createref)
21-
- [Component/Design System Development](#component-design-system-development)
22-
- [Building](#building)
23-
- [Prettier + TSLint](#prettier---tslint)
24-
- [Working with Non-Typescript Libraries (writing your own index.d.ts)](#working-with-non-typescript-libraries--writing-your-own-indexdts-)
8+
- [Section 1: Setup](#section-1--setup)
9+
* [Prerequisites](#prerequisites)
10+
* [React + Typescript Starter Kits](#react---typescript-starter-kits)
11+
* [Import React](#import-react)
12+
- [Section 2: Getting Started](#section-2--getting-started)
13+
* [Stateless Functional Components](#stateless-functional-components)
14+
* [Stateful Class-based Components](#stateful-class-based-components)
15+
* [Typing DefaultProps](#typing-defaultprops)
16+
* [Extracting Prop Types](#extracting-prop-types)
17+
* [Basic Prop Types Examples](#basic-prop-types-examples)
18+
* [Useful React Type Examples](#useful-react-type-examples)
19+
* [Forms and Events](#forms-and-events)
20+
- [Section 3: Advanced Guides](#section-3--advanced-guides)
21+
* [Higher Order Components/Render Props](#higher-order-components-render-props)
22+
* [Context](#context)
23+
* [Forwarding References/createRef](#forwarding-references-createref)
24+
* [Portals](#portals)
25+
* [Error Boundaries](#error-boundaries)
26+
* [Timeout/Placeholder/createFetcher](#timeout-placeholder-createfetcher)
27+
- [Section 4: Misc. Concerns](#section-4--misc-concerns)
28+
* [Component/Design System Development](#component-design-system-development)
29+
* [Building](#building)
30+
* [Prettier + TSLint](#prettier---tslint)
31+
* [Working with Non-Typescript Libraries (writing your own index.d.ts)](#working-with-non-typescript-libraries--writing-your-own-indexdts-)
2532
- [Troubleshooting Handbook: Types](#troubleshooting-handbook--types)
2633
* [Union types](#union-types)
2734
* [Optional Types](#optional-types)
@@ -38,26 +45,27 @@
3845

3946
</details>
4047

48+
# Section 1: Setup
4149

42-
# Prerequisites
50+
## Prerequisites
4351

4452
1. good understanding of [React](https://reactjs.org)
4553
2. familiarity with [Typescript Types](https://www.typescriptlang.org/docs/handbook/basic-types.html)
4654
3. having read [the Typescript section in the official React docs](https://reactjs.org/docs/static-type-checking.html#typescript).
4755
4. (optional) Read Microsoft's [TypeScript-React-Starter](https://github.com/Microsoft/TypeScript-React-Starter#typescript-react-starter) docs.
4856

49-
# React + Typescript Starter Kits
57+
## React + Typescript Starter Kits
5058

5159
1. <https://github.com/wmonk/create-react-app-typescript> is the officially recommended Typescript fork of `create-react-app`.
5260

53-
> CodeSandbox has a [React TypeScript template](https://codesandbox.io/s/react-ts) based on this project.
61+
> CodeSandbox has a [React TypeScript template](https://codesandbox.io/s/react-ts) based on this project. *Contributed by: [@antmdvs](https://github.com/sw-yx/react-typescript-cheatsheet/pull/11)*
5462
5563
2. <https://github.com/sw-yx/create-react-app-parcel-typescript> sets up a React + Typescript app with Parcel :)
5664
3. <https://github.com/basarat/typescript-react/tree/master/01%20bootstrap> for manual setup of React + Typescript + Webpack + Babel
5765

5866
In particular, make sure that you have `@types/react` and `@types/react-dom` installed. [Read more about the DefinitelyTyped project if you are unfamiliar](https://definitelytyped.org/).
5967

60-
# Import React
68+
## Import React
6169

6270
```tsx
6371
import * as React from 'react';
@@ -81,7 +89,11 @@ Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatshee
8189
</details>
8290

8391

84-
# Stateless Functional Components
92+
# Section 2: Getting Started
93+
94+
## Stateless Functional Components
95+
96+
*Contributed by: [@jasanst](https://github.com/sw-yx/react-typescript-cheatsheet/pull/9)*
8597

8698
You can specify the type of props as you destructure them:
8799

@@ -97,14 +109,9 @@ const App: React.SFC<{ message: string }> = ({ message }) => <div>{message}</div
97109

98110
Quite frankly I prefer the former pattern as it's shorter.
99111

100-
<details>
112+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
101113

102-
<summary>Explanation</summary>
103-
104-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
105-
</details>
106-
107-
# Stateful Class-based Components
114+
## Stateful Class-based Components
108115

109116
Within Typescript, `React.Component` is a generic type (aka `React.Component<PropType, StateType>`), so you actually want to provide it with prop and (optionally) state types:
110117

@@ -180,21 +187,19 @@ class App extends React.Component<{
180187
}
181188
}
182189
```
183-
<details>
184190

185-
<summary>Explanation</summary>
191+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
186192

187-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
188-
</details>
193+
## Typing DefaultProps
189194

190-
# Typing DefaultProps
195+
It is easy to [type defaults for functional components](https://twitter.com/GeeWengel/status/1000242363376205825), but there is some debate in the community on how to type the `static defaultProps` field for class-based components.
191196

192-
It is easy to [type defaults for functional components](https://twitter.com/GeeWengel/status/1000242363376205825), but there is some debate in the community on how to type the `static defaultProps` field for class-based components. We have an active discussion on several approaches on how to do this. [Please check out our issue here](https://github.com/sw-yx/react-typescript-cheatsheet/issues/4).
197+
**We have an active discussion on several approaches on how to do this. [Please check out our issue here](https://github.com/sw-yx/react-typescript-cheatsheet/issues/4).**
193198

194199

195-
# Extracting Prop Types
200+
## Extracting Prop Types
196201

197-
Instead of defining prop types inline, you can declare them separately (useful for reusability or code organization):
202+
Instead of defining prop types *inline*, you can declare them separately (useful for reusability or code organization):
198203

199204
```tsx
200205
type AppProps = { message: string }
@@ -224,14 +229,9 @@ class App extends React.Component<AppProps, AppState> {
224229

225230
`interface`s are different from `type`s in Typescript, but for our purposes they do the same things. [read more](https://medium.com/@martin_hotell/interface-vs-type-alias-in-typescript-2-7-2a8f1777af4c)
226231

227-
<details>
228-
229-
<summary>Explanation</summary>
232+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
230233

231-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
232-
</details>
233-
234-
# Basic Prop Types Examples
234+
## Basic Prop Types Examples
235235

236236
```tsx
237237
type AppProps = {
@@ -256,7 +256,7 @@ type AppProps = {
256256
}
257257
```
258258
259-
# Useful React Type Examples
259+
## Useful React Type Examples
260260
261261
```tsx
262262
export declare interface AppProps {
@@ -270,14 +270,9 @@ export declare interface AppProps {
270270
}
271271
```
272272

273-
<details>
274-
275-
<summary>Explanation</summary>
276-
277-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
278-
</details>
273+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
279274

280-
# Forms and Events
275+
## Forms and Events
281276

282277
This can be a bit tricky. The tooling really comes in handy here, as the @type definitions come with a wealth of typing. Type what you are looking for and usually the autocomplete will help you out. Here is what it looks like for an `onChange` for a form event:
283278

@@ -306,14 +301,11 @@ class App extends React.Component<{}, { // no props
306301
}
307302
```
308303

309-
<details>
310-
311-
<summary>Explanation</summary>
304+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
312305

313-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
314-
</details>
306+
# Section 3: Advanced Guides
315307

316-
# Higher Order Components/Render Props
308+
## Higher Order Components/Render Props
317309

318310
Sometimes you will want to write a function that can take a React element or a string or something else as a prop. The best Type to use for such a situation is `React.ReactNode` which fits anywhere a normal, well, React Node would fit:
319311

@@ -341,14 +333,11 @@ export interface Props {
341333
}
342334
```
343335

344-
<details>
336+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
345337

346-
<summary>Explanation</summary>
338+
## Context
347339

348-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
349-
</details>
350-
351-
# Context
340+
*Contributed by: [@jpavon](https://github.com/sw-yx/react-typescript-cheatsheet/pull/13)*
352341

353342
Using the new context API `React.createContext`:
354343

@@ -395,14 +384,9 @@ class Provider extends React.Component<{}, ProviderState> {
395384
const Consumer = Context.Consumer
396385
```
397386

398-
<details>
399-
400-
<summary>Explanation</summary>
401-
402-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
403-
</details>
387+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
404388

405-
# References/createRef
389+
## Forwarding References/createRef
406390

407391
Use a `React.RefObject`:
408392

@@ -415,57 +399,51 @@ class CssThemeProvider extends React.PureComponent<Props> {
415399
}
416400
```
417401

418-
<details>
419-
420-
<summary>Explanation</summary>
402+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
421403

422-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
423-
</details>
404+
## Portals
424405

425-
# Component/Design System Development
406+
*Not written yet.*
426407

427-
For developing with Storybook, read the docs I maintain over here: <https://storybook.js.org/configurations/typescript-config/>. This includes automatic proptype documentation generation, which is awesome :)
408+
[Want to contribute this section? Respond in this issue.](https://github.com/sw-yx/react-typescript-cheatsheet/issues/6)
428409

429-
<details>
410+
## Error Boundaries
430411

431-
<summary>Explanation</summary>
412+
*Not written yet.*
432413

433-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
434-
</details>
414+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
435415

436-
# Building
416+
## Timeout/Placeholder/createFetcher
437417

438-
Please contribute on this topic! [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
418+
*Not written yet.* watch <https://github.com/sw-yx/fresh-async-react> for more on React Suspense and Time Slicing.
439419

440-
<details>
420+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
441421

442-
<summary>Explanation</summary>
422+
# Section 4: Misc. Concerns
443423

444-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
445-
</details>
424+
Sometimes writing React isn't just about React. While we don't focus on other libraries like Redux (see below for more on that), here are some tips on other common concerns when making apps with React + Typescript.
446425

447-
# Prettier + TSLint
426+
## Component/Design System Development
448427

449-
Please contribute on this topic! [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
428+
For developing with Storybook, read the docs I maintain over here: <https://storybook.js.org/configurations/typescript-config/>. This includes automatic proptype documentation generation, which is awesome :)
450429

451-
<details>
430+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
452431

453-
<summary>Explanation</summary>
432+
## Building
454433

455-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
456-
</details>
434+
*Not written yet.*
457435

458-
# Working with Non-Typescript Libraries (writing your own index.d.ts)
436+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
459437

460-
Please contribute on this topic! [We have an ongoing issue here with some references](https://github.com/sw-yx/react-typescript-cheatsheet/issues/8).
438+
## Prettier + TSLint
461439

462-
<details>
440+
We have an active discussion on Linting [here](https://github.com/sw-yx/react-typescript-cheatsheet/issues/7).
463441

464-
<summary>Explanation</summary>
442+
## Working with Non-Typescript Libraries (writing your own index.d.ts)
465443

466-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
467-
</details>
444+
*Not written yet.*
468445

446+
Please contribute on this topic! [We have an ongoing issue here with some references](https://github.com/sw-yx/react-typescript-cheatsheet/issues/8).
469447

470448
# Troubleshooting Handbook: Types
471449

0 commit comments

Comments
 (0)