简体   繁体   English

使用 useRef().current 而不是 useRef() 有什么区别吗?

[英]Is there any difference between using useRef().current instead of useRef()?

I have a question about the declaration of references in React using the useRef hook.我有一个关于使用 useRef 钩子在 React 中声明引用的问题。

Is there any difference between doing this:这样做有什么区别:

 const inputRef = useRef(null).current;

 ...

 inputRef?.focus();

and this:和这个:

 const inputRef = useRef(null);
 
 ...

 inputRef.current?.focus();

?? ??

In my code the behaviour is the same but in some situations, with other use cases, I have different results.在我的代码中,行为是相同的,但在某些情况下,对于其他用例,我有不同的结果。

For example:例如:

 const arrayRef = useRef(null);
 arrayRef.current = data; // data is an array of numbers

seems not to be the same as似乎不一样

 const arrayRef = useRef(null).current;
 arrayRef = data;
 

But why?但为什么? (Not really sure if isn't the same, but my code works unexpectedly using the second method.) (不确定是否不一样,但我的代码使用第二种方法意外地工作。)

I would really appreciate your help.我将衷心感谢您的帮助。 Thank you.谢谢你。

A problem with using .current is that that'll extract the current reference immediately .使用.current一个问题是它会立即提取当前引用。 If the value is extracted at the time of render, but later a re-render occurs, but the initial render still uses the extracted reference somewhere, it'll be a reference to the old value in the ref which may not exist yet.如果该值在渲染时被提取,但后来发生了重新渲染,但初始渲染仍然在某处使用提取的引用,它将是对 ref 中可能尚不存在的旧值的引用。

For a quick example:举个简单的例子:

const Foo = () => {
  const inputRef = useRef(null);
  useEffect(() => {
    window.addEventListener('custom-focus-the-input', () => {
      inputRef.current.focus();
    });
  }, []);
  return (
    <input ref={inputRef} />
  );
};

Above, the ref must be used as a plain ref.上面,ref必须用作普通 ref。 You can't extract it into its current value until you're inside the event listener, because the ref's reference changes on first render.在进入事件侦听器之前,您无法将其提取为current值,因为 ref 的引用在第一次渲染时发生了变化。

Also, for a ref to get its .current assigned properly, you have to put the ref itself in the returned JSX - don't put the .current value into the JSX, because that won't result in the .current property of the ref changing.此外, .current ref 正确分配其.current ,您必须将 ref 本身放入返回的 JSX - 不要将.current值放入 JSX,因为这不会导致.current属性参考改变。


In general - only extract the .current value from the ref until you need to use it shortly in subsequent synchronous code.通常 - 仅从 ref 中提取.current值,直到您需要在后续同步代码中立即使用它。

useRef (like almost all react hooks) effectively supplies an instance variable to something that doesn't have an instance (a functional component isn't backed by an instance - it's a function, but react keeps track of it's lifecycle via fiber's and other cool magic). useRef (就像几乎所有的 react hooks 一样)有效地为没有实例的东西提供了一个实例变量(一个功能组件没有实例支持——它是一个函数,但是 react 通过光纤和其他很酷的东西来跟踪它的生命周期魔法)。

Now that you know it's just an instance variable, it becomes clear that instance variables are only useful if they can be assigned things.现在您知道它只是一个实例变量,很明显实例变量只有在可以赋值时才有用。

In the context you've given : 在您给出的上下文中

const animatedOpacity = useRef(new Animated.Value(0)).current;

The instance variable is assigned an Animated.Value .实例变量被分配了一个Animated.Value It never needs to be reassigned because Animated.Value (which is the current property of the ref) is manipulated/reassigned using other functions, like Animated.timing .它永远不需要重新分配,因为Animated.Value (这是 ref 的current属性)是使用其他函数操作/重新分配的,例如Animated.timing

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

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