forward-ref-as
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

forwardRefAs()

A wrapper around React.forwardRef() that allows HTML attributes and prop types to be inferred from an as prop.
npm i forward-ref-as

Bundlephobia Types Code coverage Build status NPM Version MIT License


Quick start

import forwardRefAs from 'forward-ref-as'

// Forwards `ref` to the underlying button and adds strong
// types for the `as` prop.
const Button = forwardRefAs<'button', ButtonProps>(
  ({as: As = 'button', ...props}, ref) => <As ref={ref} {...props} />
)

// ✅ Will pass type checking and autocomplete correctly
;<Button as='a' href='https://jaredLunde.com' />

API

forwardRefAs()

A wrapper around React.forwardRef() with the same call signature, but a type signature that allows as prop HTML attributes and React prop types to be inferred.

LICENSE

MIT

Package Sidebar

Install

npm i forward-ref-as

Weekly Downloads

1,905

Version

2.0.0

License

MIT

Unpacked Size

34.4 kB

Total Files

17

Last publish

Collaborators

  • jaredlunde