const store = reactive({
    a: [],
    b: computed(()=> a.find('needle')), use computed
    c: ()=> a.find('needle') // or a method
})

How should one go about using a computed property in a reactive store object?

I suppose the best practice is probably not setting the computed property in the reactive object? But would this work?

2 Replies 2

Considering the store is wrapped with reactive, computed ref is unwrapped and results in readonly property. Unless c needs to accept parameters, there's no reason for it to be a method. It benefits from being a computed because returned value is cached. So b would be the best practice. You can see this in action in Pinia stores.

I forgot to wrap the store. So I want the computed property to be in the store so I can just call store.b. Instead of having to write the computed ref seperately. So I think I'll need to do it the 'c' way. As the computed ref should be refering to a reactive object, but in the initilization state, the reactive object has not been created yet.

And yea, the proper way would be to use a proper store management system, but I was wondering if there's an easy way for it.

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.