Skip to content
On this page

createUnrefFn

Category
Last Changed
3 months ago

Make a plain function accepting ref and raw values as arguments. Returns the same value the unconverted function returns, with proper typing.

TIP

Make sure you're using the right tool for the job. Using reactify might be more pertinent in some cases where you want to evaluate the function on each changes of it's arguments.

Usage

import { ref } from 'vue'
import { createUnrefFn } from '@vueuse/core'

const url = ref('https://httpbin.org/post')
const data = ref({ foo: 'bar' })

const post = (url, data) => fetch(url, { data })
const unrefPost = createUnrefFn(post)

post(url, data)        /* ❌ Will throw an error because the arguments are refs */
unrefPost(url, data)   /* ✔️ Will Work because the arguments will be auto unref */
import { ref } from 'vue'
import { createUnrefFn } from '@vueuse/core'

const url = ref('https://httpbin.org/post')
const data = ref({ foo: 'bar' })

const post = (url, data) => fetch(url, { data })
const unrefPost = createUnrefFn(post)

post(url, data)        /* ❌ Will throw an error because the arguments are refs */
unrefPost(url, data)   /* ✔️ Will Work because the arguments will be auto unref */
  • reactify

Type Declarations

export declare type UnrefFn<T> = T extends (...args: infer A) => infer R
  ? (
      ...args: {
        [K in keyof A]: MaybeRef<A[K]>
      }
    ) => R
  : never
/**
 * Make a plain function accepting ref and raw values as arguments.
 * Returns the same value the unconverted function returns, with proper typing.
 */
export declare const createUnrefFn: <T extends Function>(fn: T) => UnrefFn<T>
export declare type UnrefFn<T> = T extends (...args: infer A) => infer R
  ? (
      ...args: {
        [K in keyof A]: MaybeRef<A[K]>
      }
    ) => R
  : never
/**
 * Make a plain function accepting ref and raw values as arguments.
 * Returns the same value the unconverted function returns, with proper typing.
 */
export declare const createUnrefFn: <T extends Function>(fn: T) => UnrefFn<T>

Source

SourceDocs

Contributors

Anthony Fu
Stanley Horwood

Changelog

v6.9.0 on 11/14/2021
08331 - feat: new function (#789)
createUnrefFn has loaded