简体   繁体   English

`let count = useRef(0).current` 的缺点是什么

[英]What's the drawback of `let count = useRef(0).current`

Example Code:示例代码:

(Pretend we are inside component function) (假设我们在组件函数内部)

let count = useRef(0).current

useEffect(()=>{
  count++
  console.log(count)
}, [count])

Question:问题:

  1. What will happen if I run this code如果我运行这段代码会发生什么

(I am afraid that the endless loop execution will blow up my m1 chip macbookair, so I didn't run ^_^). (怕死循环执行会炸掉我的m1芯片macbookair,所以没有跑^_^)。

  1. Should I awalys some_ref_object.curent = some_value to change the value?我应该使用some_ref_object.curent = some_value来更改值吗?

The code probably will not do what you expect.代码可能不会像您期望的那样。

useRef returns a mutable object, where the object is shared across renders. useRef返回一个可变的 object,其中 object 在渲染之间共享。 But if you extract a property from that object into a variable and then reassign that variable, the object won't change.但是,如果您从 object 中提取一个属性到一个变量中,然后重新分配该变量,则 object 不会改变。

For the same reason, reassigning the number below doesn't change the object:出于同样的原因,重新分配下面的数字不会改变 object:

 const obj = { foo: 3 }; let { foo } = obj; foo = 10; console.log(obj);

In your code, the ref object never gets mutated.在您的代码中,参考 object 永远不会发生突变。 It is always the following object:始终是以下 object:

{ current: 0 }

So所以

let count = useRef(0).current

results in count being initially assigned to 0 at the beginning of every render.导致count在每次渲染开始时最初分配为 0。

This might be useful if for some odd reason you wanted to keep track of a number inside a given render, not to persist for any other render - but in such a case, it'd make a lot more sense to remove the ref entirely and just do如果出于某种奇怪的原因您想跟踪给定渲染中的数字,而不是为任何其他渲染持续存在,这可能很有用 - 但在这种情况下,完全删除 ref 和做就是了

let count = 0;

Your effect hook won't do anything useful either - since count is always 0 at the start of every render, the effect callback will never run (except on the first render).您的效果挂钩也不会做任何有用的事情 - 因为每次渲染开始时count始终为 0,所以效果回调将永远不会运行(第一次渲染除外)。

Should I awalys some_ref_object.curent = some_value to change the value我是否应该使用 some_ref_object.curent = some_value 来更改值

You should use current , not curent .您应该使用current而不是curent But yes - if you want the change to persist, assign to a property of the object, instead of reassigning a standalone variable.但是是的 - 如果您希望更改持续存在,请分配给 object 的属性,而不是重新分配独立变量。 (Reassigning a standalone variable will almost never have any side-effects.) (重新分配独立变量几乎不会有任何副作用。)

But, if you have something in the view that the count is used in - for example, if you want to return it in the JSX somewhere - you probably want state instead of a ref (or a let count = 0; ), so that setting state results in the component re-rendering and the view updating.但是,如果您认为使用计数的某些内容 - 例如,如果您想在 JSX 中的某个地方返回它 - 您可能需要state而不是 ref (或let count = 0; ),这样设置 state 会导致组件重新渲染和视图更新。

I just tried it on my colleague's computer, and fortunately it didn't blow up刚在同事的电脑上试了一下,还好没炸

Conclusion 1:结论一:

The useEffect won't effect, because ref can't be denpendency. useEffect 不会生效,因为 ref 不能是依赖关系。

Conclusion 2:结论二:

Only let count = useRef(0).current is the right way.只有let count = useRef(0).current是正确的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM