Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added typesafe forward-ref component implementation #223

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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