Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nooooooom/vue-forward-ref

💫 Make it easier for HOCs to forward refs
https://github.com/nooooooom/vue-forward-ref

forward-ref hoc vue

Last synced: about 1 month ago
JSON representation

💫 Make it easier for HOCs to forward refs

Awesome Lists containing this project

README

        

# vue-forward-ref

npm package

💫 Make it easier for HOCs to forward refs.

## Installation

```bash
pnpm install vue-forward-ref
```

## Usage

`vue-forward-ref` provides two forwarding methods.

The easiest is to use `forwardRef` to wrap the component you need to forward.

```js
import { forwardRef } from 'vue-forward-ref'

defineComponent({
name: 'Wrapper',
setup() {
return () => {
// The component can be any type used to create a vnode
// - string
// - Component
// - ConcreteComponent
return forwardRef('div')
}
}
})
```

If you need to extend or override the forward, you can use `createForwardRef`.

```js
import { createForwardRef } from 'vue-forward-ref'

defineComponent({
name: 'Wrapper',
setup() {
const override = {
// ...
}
// Assign `forwardRef` to the component that needs to be forwarded,
// and the first parameter allows you to pass in the ref already defined
const forwardRef = createForwardRef(null, override)

return () => {
return h(Component, {
ref: forwardRef
})
}
}
})
```

Either way, they allow you to customize which component the declaration is forwarded on.

```js
forwardRef(Component, instance)
// or
createForwardRef(null, null, instance)
```

## License

[MIT](./LICENSE)