search

Home  >  Q&A  >  body text

vue 3 ref variable chooses between let and const

I'm creating a new Vue 3 project and I see a lot of people stating references like this online.

const myVariable = ref(false)

Why are we suddenly using const in Vue 3?

I know that refs wrap them somehow to make them editable, but I still don't understand why they are not declared like this:

let myVariable = ref(false)

I know this may sound silly to a Vue 3 developer, but I can't understand the reason behind changing the value to a constant.

Meanwhile, I'm using const declarations in the composition API, but I'd like to understand the reasoning behind it

P粉463418483P粉463418483408 days ago526

reply all(1)I'll reply

  • P粉872182023

    P粉8721820232023-12-26 12:30:05

    This is preference, but the argument for using const is when the value has not changed, for example:

    const name = 'John';
    
    // Shouldn't work.
    name = 'Bill';

    With ref() you are not replacing variables, you are replacing properties

    const name = ref('John');
    
    name.current = 'Bill';

    The following is the explanation of eslint:

    Documentation (at the time of writing): https://eslint.org/docs/latest/rules/prefer-const

    reply
    0
  • Cancelreply