script/no-reactive-destructure
Disallow destructuring reactive objects which loses reactivity
Default severity: warning
Presets: none
Automatic fix: None; review the suggested change
Applies to: JS/TS scripts in Vue SFCs; examples show the relevant Options API or script setup form
Options: No rule-specific options. Severity and preset selection are configurable.
Configuration (Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({
lint: {
vize: {
"preset": "incremental",
"rules": {
"script/no-reactive-destructure": "warn"
}
},
},
});
vp run lint
Bad
const { count, name } = state copies primitive properties out of the reactive object, losing their connection to subsequent property changes.
<script setup lang="ts">
import { reactive } from "vue";
const state = reactive({ count: 0, name: "Ada" });
const { count, name } = state;
</script>
Good
Destructuring toRefs(state) creates refs for count and name, keeping each binding linked to the original reactive property.
<script setup lang="ts">
import { reactive, toRefs } from "vue";
const state = reactive({ count: 0, name: "Ada" });
const { count, name } = toRefs(state);
</script>
Good avoids this rule's finding under the configuration above; other rules may still report diagnostics.