-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added typesafe forward-ref component implementation
- Loading branch information
1 parent
74c08da
commit d45d48b
Showing
4 changed files
with
43 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Usage: | ||
```jsx { "filePath": "./fc-forward-ref.usage.tsx" } | ||
``` | ||
|
||
Usage Demo: | ||
```jsx | ||
const Demo = require('./fc-forward-ref.usage').default; | ||
<Demo /> | ||
``` | ||
|
||
[⇦ back to guide](https://github.com/piotrwitek/react-redux-typescript-guide#--fc-counter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
|
||
export type FancyButtonProps = { | ||
className?: string; | ||
children?: React.ReactNode; | ||
} & React.ButtonHTMLAttributes<HTMLButtonElement>; | ||
|
||
// using function DevTools will show "ForwardRef(FancyButton)" instead of just "ForwardRef" | ||
export const FancyButton = React.forwardRef< | ||
HTMLButtonElement, | ||
FancyButtonProps | ||
>(function FancyButton(props, ref) { | ||
return ( | ||
<button ref={ref} className="FancyButton" {...props}> | ||
{props.children} | ||
</button> | ||
); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import React from 'react'; | ||
|
||
import { FancyButton } from '.'; | ||
|
||
const ref = React.createRef<HTMLButtonElement>(); | ||
export default () => <FancyButton ref={ref}>Click me!</FancyButton>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters