This repository was archived by the owner on Nov 8, 2024. It is now read-only.
File tree 1 file changed +22
-4
lines changed 1 file changed +22
-4
lines changed Original file line number Diff line number Diff line change 1
1
- Props declaration
2
2
``` typescript
3
- const upgradeCTA = ({ label }: { label? : string }) : React .ReactNode => …
4
- // Theoretically we could skip the type definition in this case
5
- // as it will be infered from the default values
3
+ const upgradeCTA = ({ label = ' upgrade' }: { label? : string }) : React .Element => …
6
4
```
7
5
- useful React props types ([ ref] ( https://github.com/typescript-cheatsheets/react#useful-react-prop-type-examples ) .)
8
6
``` typescript
9
7
export declare interface AppProps {
10
- children: React .ReactNode ; // best, accepts everything (see edge case below)
8
+ children: React .ReactNode ;
11
9
functionChildren: (name : string ) => React .ReactNode ; // recommended function as a child render prop type
12
10
style? : React .CSSProperties ; // to pass through style props
13
11
onChange? : React .FormEvent <HTMLInputElement >; // form events! the generic parameter is the type of event.target
35
33
// To fix it, you need to specify the Type of the prop
36
34
margin : 2px 0 $ {({ content } : { content: Detail }) => content .images ? ' 24px' : ' 0px' } 0 ;
37
35
```
36
+
37
+ - Generics
38
+ ``` typescript
39
+ import React , { useState } from ' react'
40
+
41
+ // For props
42
+ interface WithName {
43
+ name: string | null ;
44
+ }
45
+
46
+ const Foo: React .FC <WithName > = ({ name }) => {
47
+ return <div >{name }< / div >
48
+ }
49
+
50
+ // To strongly type hooks
51
+ const Bar: React .FC = () => {
52
+ const [state, setState] = useState <WithName >({ name: ' bar' })
53
+ return <div >{state.name }< / div >
54
+ }
55
+ ```
You can’t perform that action at this time.
0 commit comments