From d5b7133702ef9af1a30ebd646c756d48c47467ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20H=C3=BCbelbauer?= Date: Fri, 8 Jun 2018 09:58:10 +0200 Subject: [PATCH 1/3] Contribute an alternative typing example for forms sample --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index 194c61ee..f997d2a8 100644 --- a/README.md +++ b/README.md @@ -369,6 +369,32 @@ class App extends React.Component<{}, { // no props } ``` +You can also use React's event handler types like this: + +```tsx +class App extends React.Component<{}, { // no props + text: string, + }> { + state = { + text: '' + } + render() { + return ( +
+ +
+ ); + } + onChange: React.ChangeEventHandler = (e) => { + this.setState({text: e.currentTarget.value}) + } +} +``` + [Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new). # Section 3: Advanced Guides From 1bc5838cc183a5270db4a32f40a2e2bbb908bdd9 Mon Sep 17 00:00:00 2001 From: shawn wang Date: Fri, 8 Jun 2018 10:44:31 -0400 Subject: [PATCH 2/3] shorten React.ChangeEventHandler example and move to discussion shorten React.ChangeEventHandler example and move to discussion --- README.md | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index f997d2a8..7c13af07 100644 --- a/README.md +++ b/README.md @@ -369,33 +369,21 @@ class App extends React.Component<{}, { // no props } ``` -You can also use React's event handler types like this: +Instead of typing the arguments and return values with `React.FormEvent<>` and `void`, you can may alternatively apply types on to the event handler itself (*contributed by @TomasHubelbauer*): ```tsx -class App extends React.Component<{}, { // no props - text: string, - }> { - state = { - text: '' - } - render() { - return ( -
- -
- ); - } onChange: React.ChangeEventHandler = (e) => { this.setState({text: e.currentTarget.value}) } -} ``` -[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new). +
+ +Discussion + +Why two ways to do the same thing? The first method uses an inferred method signature `(e: React.FormEvent): void` and the second method enforces a type of the delegate provided by `@types/react`. So `React.ChangeEventHandler<>` is simply a "blessed" typing by `@types/react`, whereas you can think of the inferred method as more... *artisanally hand-rolled*. Either way it's a good pattern to know. [See our Github PR for more](https://github.com/sw-yx/react-typescript-cheatsheet/pull/24). + +
# Section 3: Advanced Guides From 467276989b8c2603035173bebbcb0d72b65dee94 Mon Sep 17 00:00:00 2001 From: shawn wang Date: Fri, 8 Jun 2018 10:45:53 -0400 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7c13af07..ddc6b6c1 100644 --- a/README.md +++ b/README.md @@ -369,7 +369,7 @@ class App extends React.Component<{}, { // no props } ``` -Instead of typing the arguments and return values with `React.FormEvent<>` and `void`, you can may alternatively apply types on to the event handler itself (*contributed by @TomasHubelbauer*): +Instead of typing the arguments and return values with `React.FormEvent<>` and `void`, you may alternatively apply types to the event handler itself (*contributed by @TomasHubelbauer*): ```tsx onChange: React.ChangeEventHandler = (e) => {