3 ms·
is reactive() just a mere wrapper around ref() ?
by jaequery 6y ago
is reactive() just a mere wrapper around ref() ?
- gavinray 6y agoKind of -- reactive() lets you declare multiple values at once, and you can read the property directly. ref() is for single values, and to get at the actual VALUE, you need to use "myRef.value" const state = reactive({ msg: "hello", count: 1 }) state.msg // "hello" const msg = ref("hello") msg.value // "hello" Though when you use ref()'s in templates, it will bind to .value for you.
- mthoms 6y agoI find myself using `const state = reactive({...})` exclusively now. It's just way simpler (read:less mental overhead). But, I've only been using the composition API for a short while now. Do experienced Vue devs actually use both regularly?
- gavinray 6y agoPersonally, I rarely use refs. I think they have more use when creating general-purpose libraries for Vue. You can also just call "toRefs()" on a "reactive()" object though. Refs are also useful if you want to destructure a reactive object without breaking the reactivity, scroll down just below this: https://composition-api.vuejs.org/api.html#torefs https://composition-api.vuejs.org/api.html#torefs
- Exuma 6y agoAccording to the vue discord where I asked a lot of composition API questions, pretty much people use ref exclusively. It's much more explicit in your `use*` composition files when you have to call .value. It's rare to me to export a very large object of keys, and I have yet to use it yet.
- likeclockwork 6y agoOther way around I think. ref is a single value reactive.