Skip to content

Commit

Permalink
Added typesafe forward-ref component implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrwitek committed May 3, 2022
1 parent 74c08da commit d45d48b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
11 changes: 11 additions & 0 deletions playground/src/components/fc-forward-ref.md
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)
19 changes: 19 additions & 0 deletions playground/src/components/fc-forward-ref.tsx
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>
);
});

6 changes: 6 additions & 0 deletions playground/src/components/fc-forward-ref.usage.tsx
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>;
11 changes: 7 additions & 4 deletions playground/src/routes/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import React from 'react'
import React from 'react';

import ClassCounterWithDefaultPropsUsage from '../components/class-counter-with-default-props.usage';
import ClassCounterUsage from '../components/class-counter.usage';
import FCCounterUsage from '../components/fc-counter.usage';
import FcForwardRefUsage from '../components/fc-forward-ref.usage';
import FCSpreadAttributesUsage from '../components/fc-spread-attributes.usage';
import ClassCounterUsage from '../components/class-counter.usage';
import ClassCounterWithDefaultPropsUsage from '../components/class-counter-with-default-props.usage';
import UserListUsage from '../components/generic-list.usage';
import WithConnectedCountUsage from '../hoc/with-connected-count.usage';
import WithErrorBoundaryUsage from '../hoc/with-error-boundary.usage';
import WithStateUsage from '../hoc/with-state.usage';
import WithConnectedCountUsage from '../hoc/with-connected-count.usage';

export function Home() {
return (
<section>
<FCCounterUsage />
<FcForwardRefUsage />
<FCSpreadAttributesUsage />
<ClassCounterUsage />
<ClassCounterWithDefaultPropsUsage />
Expand Down

0 comments on commit d45d48b

Please sign in to comment.