Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

298
Views
Vue 3 - differences between reactive value return - toRef and computed

Good afternoon. I have a reactive state in one of my composable:

const userState = reactive({
  email: 'test@test.com',
  name: '',
  ...other
})

and I want to return the email property. This is how I thought to return it:

return {
  email: readonly(toRef(userState, 'email'),
  emailComputed: computed(() => userState.email)
}

but in that case I don't understand the difference between returning it as readonly+ toRef and computed, since it looks they keep the reactivity outside the composable.

Any difference I am missing about?

Thanks

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In my opinion when you return a variable from a composable, that means you want to work with that variable as an output of composable in your components. So when you return it as readonly, that means you don't want to change it during other operations in the component and if you change that Vue create warning for you. But when you return it as computed that means you want to have the value of your main variable (here called userState), but also if needed you change it in your component. Here I posted the codes of my component and composable to determine the above sentences clearly:

component code:

<template>
  <div>
    <p>{{ emailComputed }}</p>
    <p> {{ email }} </p>
    <button @click="changeEmail">change email</button>
  </div>
</template>

<script>
/* importing composable */
import {useReadOnly} from "../composables/codeReadOnly";
import {reactive} from "vue";
export default {
  setup() {
   
    const { email, emailComputed } = useReadOnly();

    const changeEmail = function () {
      /* uncomment the line below and comment second line */
      // ----------------------------------------------------
      // emailComputed.value = "new-email@email.com";
      email.value = "new-email@email.com";
    }

    return {
      emailComputed,
      email,
      changeEmail
    }

  }
}
</script>

<style scoped>

</style>

composable code:

import {reactive, computed, toRef, readonly} from "vue";
export function useReadOnly() {
    const userState = reactive({
        email: 'test@test.com',
        name: "",
    })

    return {
        email: readonly(toRef(userState, 'email')),
        emailComputed: computed({
            get: () => userState.email,
            set: (val) => {
                userState.email = val;
            }
        })
}
}

In the above code you can change the value of emailComputed by clicking the button, but you can not change the value of email in your main component. That is the difference I could understand, but maybe there are other differences also that I don't know.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!