script/prefer-ref-over-reactive
状態管理に reactive() より ref() を使う方針を適用します。
既定の重大度: warning
プリセット: none
自動修正: なし。修正内容を確認してください
適用範囲: Vue SFC の JS / TS script。Options API または script setup の対象は例を参照してください。
オプション: ルール固有のオプションはありません。重大度とプリセットは設定できます。
設定(Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({
lint: {
vize: {
"preset": "incremental",
"rules": {
"script/prefer-ref-over-reactive": "warn"
}
},
},
});
vp run lint
悪い
状態を reactive で作り、この意見を持つルールが推奨する ref を使っていません。これはスタイルの推奨を示す例であり、reactive オブジェクト自体が不正という意味ではありません。
<script setup lang="ts">
// reactive requires careful handling to avoid losing reactivity
const state = reactive({
count: 0,
name: 'foo'
})
</script>
良い
スカラーとオブジェクトの状態をどちらも ref で作ります。関連するフィールドを個別の ref に分ける例も含め、推奨する状態の作成形式にそろえます。
<script setup lang="ts">
// ref is more explicit and safer
const count = ref(0)
const name = ref('foo')
// For objects, ref still works
const user = ref({ name: 'foo', age: 20 })
// Or use multiple refs for related data
const userName = ref('foo')
const userAge = ref(20)
</script>
良い例は上記の設定でこのルールの検出を避ける例です。他のルールでは検出される場合があります。