File tree Expand file tree Collapse file tree 1 file changed +18
-4
lines changed Expand file tree Collapse file tree 1 file changed +18
-4
lines changed Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ Translations:
42
42
* [ Useful React Prop Type Examples] ( #useful-react-prop-type-examples )
43
43
* [ Forms and Events] ( #forms-and-events )
44
44
* [ Context] ( #context )
45
- * [ Forwarding References /createRef] ( #forwarding-referencescreateref )
45
+ * [ forwardRef /createRef] ( #forwardrefcreateref )
46
46
* [ Portals] ( #portals )
47
47
* [ Error Boundaries] ( #error-boundaries )
48
48
* [ Concurrent React/React Suspense] ( #concurrent-reactreact-suspense )
@@ -535,19 +535,33 @@ const Consumer = Context.Consumer
535
535
536
536
[ Something to add? File an issue] ( https://github.com/sw-yx/react-typescript-cheatsheet/issues/new ) .
537
537
538
- ## Forwarding References /createRef
538
+ ## forwardRef /createRef
539
539
540
- Use a ` React.RefObject ` :
540
+ ` createRef ` :
541
541
542
542
``` tsx
543
543
class CssThemeProvider extends React .PureComponent <Props > {
544
- private rootRef = React .createRef <HTMLDivElement >();
544
+ private rootRef = React .createRef <HTMLDivElement >(); // like this
545
545
render() {
546
546
return <div ref = { this .rootRef } >{ this .props .children } </div >;
547
547
}
548
548
}
549
549
```
550
550
551
+ ` forwardRef ` :
552
+
553
+ ``` tsx
554
+ type Props = { children: React .ReactNode ; type: ' submit | ' button ' }
555
+ export type Ref = HTMLButtonElement
556
+ export const FancyButton = React .forwardRef <Ref , Props >((props , ref ) => (
557
+ <button ref = { ref } className = " MyClassName" type = { props .type } >
558
+ { props .children }
559
+ </button >
560
+ ))
561
+ ` ` `
562
+
563
+ More info: https://medium.com/@martin_hotell/react-refs-with-typescript-a32d56c4d315
564
+
551
565
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
552
566
553
567
## Portals
You can’t perform that action at this time.
0 commit comments