I am trying to use a Pinia state store to keep an object which uses Vue refs inside it.
The object is an instance of this class (simplified example):
class Foo {
constructor() {
this.field = ref('')
}
setField(value) {
this.field.value = value
}
}
const foo = new Foo
foo.setField('bar') // all happy — no errors here
(I use those kind of objects as data records, and the fields need to be refs because they are rendered in templates and I want them reactive).
So, I define a Pinia store and set the object to it:
const useState = defineStore('main', {
state: () => {
return {
foo: null
}
}
})
const state = useState()
state.foo = foo
Now, when I try to operate on the object having retrieved it from the store, I get:
state.foo.setField('baz') // this fails with the error
TypeError: Cannot create property 'value' on string 'bar'
What I guess happens here is that Pinia dereferences the field
property on the foo
object when it is accessed from the store. But heck, I do not need that! I want the foo
object behave consistently no matter where it is retrieved from.
Any idea how to do that? I see Pinia has a method called skipHydrate
which might be about this stuff, but I can't find how to use it.