vue 3 ref variable chooses between let and const
P粉463418483
P粉463418483 2023-12-25 13:40:58
0
1
504

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粉463418483
P粉463418483

reply all(1)
P粉872182023

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template