Vue Reactivator
vue-reactivator is a lightweight (0.5KB minified & gzipped) Vue mixin designed to integrate arbitrary, non-reactive global state into Vue components, making it reactive. Currently in stable version 3.0.2, releases are infrequent, typically tied to minor feature enhancements, bug fixes, or breaking changes like dropping old Node.js versions. Its primary differentiation lies in providing a flexible framework for developers to create custom "implementations" that bridge external mutable state (e.g., browser viewport size, external APIs, legacy JavaScript libraries) with Vue's reactivity system. This allows components to react to changes happening outside the Vue ecosystem without complex manual watchers or event bus setups, offering a clean way to keep UI in sync with global data sources that are not inherently Vuex stores or similar dedicated state management solutions.
Common errors
-
TypeError: reactivator is not a function
cause Attempting to use `reactivator` directly as a mixin without calling it as a factory function, or incorrect import leading to `undefined`.fixEnsure you are calling `reactivator` with an object of state implementations, like `mixins: [reactivator({ myState: myImplementation })]`. Also verify the import path and style (`import reactivator from 'vue-reactivator'` for ESM). -
ReferenceError: vueReactivator is not defined
cause Trying to access the global `vueReactivator` variable in a module-based environment (e.g., Webpack, Vite) without actually including the CDN script or incorrectly configuring the build.fixIf using a module bundler, use `import reactivator from 'vue-reactivator'`. If you intend to use the CDN, ensure the script tag is included before your application code, and your code accesses `window.vueReactivator`. -
TypeError: Cannot read properties of undefined (reading 'state') or similar related to `implementation`
cause An invalid or `null`/`undefined` implementation object was passed to `reactivator`, meaning it couldn't find expected properties or methods to create the reactive state.fixDouble-check that the object passed to `reactivator` contains valid state implementations, for example, `reactivator({ myReactiveProp: someValidImplementationObject })`. Ensure `someValidImplementationObject` is correctly imported and structured as expected by Reactivator.
Warnings
- breaking Version 2.0.0 dropped support for Node.js 8. Ensure your Node.js environment is version 12.0.0 or higher when upgrading.
- gotcha Reactivator itself is a framework and does not provide reactive state on its own. It requires a separate 'implementation' (like `vue-browser-state` or a custom one) to define the global state sources it should reactivate.
- gotcha For compatibility with older browsers like Internet Explorer, `vue-reactivator` may require transpilation to ES5 and inclusion of necessary polyfills (e.g., for `Object.entries`).
- gotcha In versions prior to 3.0.2, incorrect listener removal for store instances could lead to runtime errors. This was fixed to ensure proper tracking and cleanup.
Install
-
npm install vue-reactivator -
yarn add vue-reactivator -
pnpm add vue-reactivator
Imports
- reactivator
const reactivator = require('vue-reactivator').defaultimport reactivator from 'vue-reactivator'
- reactivator
import reactivator from 'vue-reactivator'
const reactivator = require('vue-reactivator') - vueReactivator (global)
import { vueReactivator } from 'vue-reactivator'<script src="https://unpkg.com/vue-reactivator"></script> // Access as window.vueReactivator
Quickstart
<template>
<div>
Your browser viewport dimensions are {{ size[0] }}x{{ size[1] }} pixels.
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import reactivator from 'vue-reactivator';
// For this example, 'vue-browser-state' is a common accompanying package.
// You would install it: `npm install vue-browser-state`
import { viewportSize } from 'vue-browser-state';
export default defineComponent({
mixins: [
reactivator({
size: viewportSize
})
],
setup() {
// In Composition API, this.size would be available after mixin application.
// For Options API, it's directly available in template/methods/computed.
}
});
</script>