‘useRef’ 存在的目的是什么?它的预期用途是什么?

huangapple go评论74阅读模式
英文:

Why does 'useRef' exist? What's it intended for?

问题

I recently ran into a problem in a project I didn't create, where I needed to collect renders from child elements for the purpose of pooling them before querying a server.
The intent was to prevent a full UI refresh, which requires querying the server, after each user interaction. So I pooled them until the user stopped making changes for a short moment and then query the server.

Initially I used useState because I'm still very new to React and it was all I knew of, besides useEffect.
So I was quite confused when everything broke afterwards. "Broke" meaning that after any interaction was "pooled" a render happened, and it took quite a while to find out that useState caused that.
The render was a problem because the parent element didn't have the updated state of the children at that point yet so it rendered them wrong. The child elements rendered themselves fine, which is why I needed to prevent the parent's render.

So I was looking for an alternative that would not cause a render on each update. Which made me eventually run into useRef.

Except that useRef not only doesn't cause a render, it also doesn't cause a dependency trigger...
At which point I'm confused and wonder why I should use it at all, because at that point it's a bothersome way of using a normal variable.
<var>.current = vs <var> =

I thought the whole point of React was the dependency chain, and with useRef not supporting that I'm at a loss why it exists...

Which is why I'd appreciate if anyone could explain to me or link me an explanation (except the official docs, already read that, still don't get it), why useRef is that way, and why I shouldn't just use a "normal" variable instead.

PS: I don't need a solution to the initial problem, I've by now worked around the lack of dependency updates. This is just for understanding and the situation that led into the confusion.

英文:

I recently ran into a problem in a project I didn't create, where I needed to collect renders from child elements for the purpose of pooling them before querying a server.
The intent was to prevent a full UI refresh, which requires querying the server, after each user interaction. So I pooled them until the user stopped making changes for a short moment and then query the server.

Initially I used useState because I'm still very new to React and it was all I knew of, besides useEffect.
So I was quite confused when everything broke afterwards. "Broke" meaning that after any interaction was "pooled" a render happened, and it took quite a while to find out that useState caused that.
The render was a problem because the parent element didn't have the updated state of the children at that point yet so it rendered them wrong. The child elements rendered themselves fine, which is why I needed to prevent the parent's render.

So I was looking for an alternative that would not cause a render on each update. Which made me eventually run into useRef.

Except that useRef not only doesn't cause a render, it also doesn't cause a dependancy trigger...
At which point I'm confused and wonder why I should use it at all, because at that point it's a bothersome way of using a normal variable.
&lt;var&gt;.current = vs &lt;var&gt; =

I thought the whole point of React was the dependancy chain, and with useRef not supporting that I'm at a loss why it exists...

Which is why I'd appreaciate if anyone could explain to me or link me an explaination (except the official docs, already read that, still don't get it), why useRef is that way, and why I shouldn't just use a "normal" variable instead.

PS: I don't need a solution to the initial problem, I've by now worked around the lack of dependancy updates. This is just for understanding and the situation that led into the confusion.

答案1

得分: 2

useRef 不同于 React 中使用 useState 管理的正常状态变量。useState 用于存储会在更新时引起重新渲染的状态,而 useRef 用于存储可变值,可以在渲染之间保持不变而不引起重新渲染。

useRef 在以下情况下很有用:

  • 访问 DOM 元素:通常用于创建对 DOM 元素的引用,以便可以直接与它们交互,例如,聚焦输入元素。

  • 存储先前状态:它可以存储变量的先前状态或值,在需要比较当前值与先前值而不引起重新渲染的情况下非常有用。

  • 存储不影响渲染的值:有时您可能需要存储不属于组件渲染输出的值,例如计时器、间隔或订阅。在这种情况下,使用 useRef 更合适,因为它不会引起重新渲染。

useRef 存在的主要原因是提供一种在渲染之间管理值而不引起不必要更新的方法。尽管可以使用“正常”变量,但它不会在渲染之间保持不变,因为组件函数会再次执行,变量将被重新初始化。

总之,useRef 用于管理在渲染之间保持不变而不引起重新渲染的值,而 useState 用于管理影响组件输出并在更新时引起重新渲染的状态。它们有不同的用途,了解何时使用其中之一非常重要,以编写高效的 React 组件。

英文:

useRef is like you said different from normal state variables managed with useState in React. While useState is designed to store state that causes re-renders when updated, useRef is designed to store mutable values that persist across renders without causing a re-render.

useRef is useful in the following situations:

  • Accessing DOM elements: It is often used to create references to DOM elements so that you can interact with them directly, e.g., focusing an input element.

  • Storing the previous state: It can store the previous state or value of a variable, which is useful in scenarios where you need to compare the current value with the previous value without causing re-renders.

  • Storing values that don't affect rendering: Sometimes you might need to store values that are not part of the component's render output, e.g., timers, intervals, or subscriptions. In this case, using useRef is more suitable because it doesn't cause a re-render.

The main reason why useRef exists is to provide a way to manage values across renders without causing unnecessary updates. Although you can use a "normal" variable, it will not persist across renders as the component function will be executed again, and the variable will be re-initialized.

In summary, useRef is used for managing values that persist across renders without causing re-renders, while useState is used for managing state that affects the component's output and should cause re-renders when updated. They serve different purposes, and understanding when to use one over the other is essential for writing efficient React components.

huangapple
  • 本文由 发表于 2023年4月11日 15:39:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定