Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

267
Visualizações
Why using toRef(props) and is it good idea to change their value in CompositionAPI?

I've seen a pattern of using props in of CompositionAPI very often, that is use toRefs to make all entries of props ref. I'm kind of confused by it.

For exmaple, from the Vue 3 official guide:

export default {
  props: {
    user: {
      type: String,
      required: true
    }
  },
  setup(props) {
    const { user } = toRefs(props)

    //... use user's value
  }
}

I have 2 questions in 2 scenearios:

  1. when the props.user is already reactive
    Its value will change if it's changed in any ancestors, so why we need to use toRefs? Doesn't it already reactive?

  2. if it's not reactive, it's just a primitive string value
    Does making it reactive means we're going to change its value? I think making a object reactive also imply that its value is welcomed to be changed. But all the guides and linters warn us that we'd better not to change the props value.(for not writing to the computed value or something)

If I can change the props value directly in the component, I no longer need to emit the changes to parent component everytime. It's very convenient but I don't know whenther it is a good idea to change the props value after we're sure it becomes reactive?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since props aren't supposed to be mutated, this is useful for a specific case that is explained in the documentation; a ref that is computed from a prop needs to be passed as an argument:

const { user } = toRefs(props)
// or
// const user = computed(() => props.user)

someFunction(user);

Where a function makes use of composition API or just needs an argument to be passed by reference rather than by value due to the way it works, e.g.:

function someFunction(val) {
  setTimeout(() => {
    console.log('Up-to-date value:', unref(val));
  }, 1000);
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda