vue-reactive-refs

Make $refs reactive so they can be used in computed properties and watchers
33,077 159
Install
npm install vue-reactive-refs
Latest Version:0.0.2
Vue:2
License:MIT
Last Updated:Mar 4, 2023
Links: GitHub  ·  npm
Maintainer: posva

vue-reactive-refs Build Status npm package coverage thanks

Make $refs reactive so they can be used in computed properties and watchers

Extremely Light < 0.2kb 🗜

Installation

npm install vue-reactive-refs

Usage

This library exposes two different plugins: ReactiveRefs and DynamicReactiveRefs and you should only use one of them

ReactiveRefs

Supports all browsers but requires you to manually declare refs in component options.

import { ReactiveRefs } from 'vue-reactive-refs'
import Vue from 'vue'

Vue.use(ReactiveRefs)

MyComponent.vue

<template>
  <div>
    <input ref="input" />
    <button v-for="button in actions" ref="buttons">{{ action }}</button>
  </div>
</template>

<script>
export default {
  // this is necessary
  refs: ['input', 'buttons'],

  computed: {
    // NOTE: this is definitely not what you should use this lib for, but it's
    // the easiest example
    inputValue() {
      return this.$refs.input && this.$refs.input.value
    },
    // Same for this example. It's ALWAYS better to mapping your data (the source of truth)
    // and avoid at all cost mapping information to the DOM
    buttonsText() {
      return this.$refs.buttons && this.$refs.buttons.map(b => b.innerText)
    },
  },
}
</script>

DynamicReactiveRefs

Supports modern browsers (not IE) that support Proxy and works out of the box:

import { DynamicReactiveRefs } from 'vue-reactive-refs'
import Vue from 'vue'

Vue.use(DynamicReactiveRefs)

MyComponent.vue

<template>
  <div>
    <input ref="input" />
    <button v-for="button in actions" ref="buttons">{{ action }}</button>
  </div>
</template>

<script>
export default {
  computed: {
    // NOTE: this is definitely not what you should use this lib for, but it's
    // the easiest example
    inputValue() {
      return this.$refs.input && this.$refs.input.value
    },
    // Same for this example. It's ALWAYS better to mapping your data (the source of truth)
    // and avoid at all cost mapping information to the DOM
    buttonsText() {
      return this.$refs.buttons && this.$refs.buttons.map(b => b.innerText)
    },
  },
}
</script>

Related

License

MIT

Related Packages

@lytjs/reactivity

LytJS reactive system - signals, refs, computed, watch, and effects

7,500
vue-storage-watcher

A reactive localStorage plugin for vue

2,147 59
@antik-web/computed-route-param

A facade for using computed property to set primitive route param values.

1,139 0
svue

Svue is a small (~2kb) library bringing Vue-style reactive stores with data and...

1,071 8
smart-observe

smart-observe comes from Vue.js and is a small, efficient library for observing...

645 37

Version History

Version Released License
0.0.2 MIT
0.0.1 MIT
0.0.0 MIT