Skip to content

Instantly share code, notes, and snippets.

@jeongtae
Last active May 30, 2023 04:50
Show Gist options
  • Save jeongtae/588627882a464effa070c16f1c47e788 to your computer and use it in GitHub Desktop.
Save jeongtae/588627882a464effa070c16f1c47e788 to your computer and use it in GitHub Desktop.
[Vue composable] This composable helps the argument of Vue Composable guarantee Vue Reactivity.
import { MaybeRef } from '@vueuse/core';
import { computed, ComputedRef, unref } from 'vue';
/**
* Union generic type of `Raw Value` | `Ref Value`, `Value Getter`.
* `Raw 값`, `Ref 값`, `값 Getter` 타입을 모두 포용하는 Union Generic 타입입니다.
*/
export type ReactiveParam<T> = T extends Function ? never : MaybeRef<T> | (() => T);
/** Unwraps `ReactiveParam<T>` and converts it to `ComputedRef<T>`.
* This function helps the argument of Vue Composable guarantee Vue Reactivity.
* `ReactiveParam<T>` 타입을 unwrap하여 `ComputedRef<T>`로 만들어줍니다.
* 이는 Vue Composable 함수 매개변수가 Vue Reactivity를 보장하도록 돕습니다.
* @link https://vuejs.org/guide/reusability/composables.html#input-arguments
*/
export function useReactiveParam<T>(param: ReactiveParam<T>): ComputedRef<T> {
return computed(() => (param instanceof Function ? param() : unref(param)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment