Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/reactivePick.md
1---2category: Reactivity3---45# reactivePick67Reactively pick fields from a reactive object.89## Usage1011### Basic Usage1213```ts14import { reactivePick } from '@vueuse/core'1516const obj = reactive({17x: 0,18y: 0,19elementX: 0,20elementY: 0,21})2223const picked = reactivePick(obj, 'x', 'elementX') // { x: number, elementX: number }24```2526### Predicate Usage2728```ts29import { reactivePick } from '@vueuse/core'3031const source = reactive({32foo: 'foo',33bar: 'bar',34baz: 'baz',35qux: true,36})37const state = reactivePick(source, (value, key) => key !== 'bar' && value !== true)38// { foo: string, baz: string }39source.qux = false40// { foo: string, baz: string, qux: boolean }41```4243### Scenarios4445#### Selectively passing props to child4647```vue48<script setup lang="ts">49import { reactivePick } from '@vueuse/core'5051const props = defineProps<{52value: string53color?: string54font?: string55}>()5657const childProps = reactivePick(props, 'color', 'font')58</script>5960<template>61<div>62<!-- only passes "color" and "font" props to child -->63<ChildComp v-bind="childProps" />64</div>65</template>66```6768#### Selectively wrap reactive object6970Instead of doing this7172```ts73import { useElementBounding } from '@vueuse/core'74import { reactive } from 'vue'7576const { height, width } = useElementBounding() // object of refs77const size = reactive({ height, width })78```7980Now we can just have this8182```ts83import { reactivePick, useElementBounding } from '@vueuse/core'8485const size = reactivePick(useElementBounding(), 'height', 'width')86```8788## Type Declarations8990```ts91export type ReactivePickReturn<T extends object, K extends keyof T> = {92[S in K]: UnwrapRef<T[S]>93}94export type ReactivePickPredicate<T> = (95value: T[keyof T],96key: keyof T,97) => boolean98export declare function reactivePick<T extends object, K extends keyof T>(99obj: T,100...keys: (K | K[])[]101): ReactivePickReturn<T, K>102export declare function reactivePick<T extends object>(103obj: T,104predicate: ReactivePickPredicate<T>,105): ReactivePickReturn<T, keyof T>106```107