简体   繁体   English

在React Hook useCallback中,如何使用(a,b)

[英]In React Hook useCallback, How are (a,b) used

In the react hooks doc, they give an example of using the useCallback React hook as follows: 在react hooks文档中,他们给出了使用useCallback React hook的示例,如下所示:

const memoizedCallback = useCallback(
  () => {
    doSomething(a, b);
  },
  [a, b],
);

I have an example that has a callback that gets called with a parameter (not a or b) and it seems to work. 我有一个示例,该回调具有使用参数(而不是a或b)调用的回调,并且似乎可以正常工作。 Please explain to me what a,b are and how they are meant to be used. 请向我解释a,b是什么以及如何使用它们。 Below is my working code using a callback. 下面是我使用回调的工作代码。

const signupCallback = email => {
  return console.log(`sign up called with email ${email}`);
};

const memoizedsignupCallback = useCallback(() => {
  signupCallback();
}, []);

and the call that uses the callback. 以及使用回调的调用。

<SignMeUp signupCallback={memoizedsignupCallback} />

This is the array of values that the hook depends on. 这是挂钩依赖的值的数组。 When these values change, it causes the hook to re-execute. 当这些值更改时,它将导致挂钩重新执行。 If you don't pass this parameter, the hook will evaluate every time the component renders. 如果不传递此参数,则挂钩将在每次组件渲染时进行评估。 If you pass in [] , it will only evaluate on the initial render. 如果传入[] ,它将仅在初始渲染时求值。

Documentation regarding this is available here: https://reactjs.org/docs/hooks-reference.html#conditionally-firing-an-effect . 与此相关的文档可在以下位置找到: https : //reactjs.org/docs/hooks-reference.html#conditionally-firing-an-effect

If you do pass this array of parameters, it is very important to include all of the state that can change and is referenced in the hook closure. 如果您确实传递了此参数数组,则包含所有可以更改的状态以及在钩子闭包中引用的所有状态非常重要。 If you forget to include something, the values in the closure will become stale. 如果您忘记包含某些内容,则闭包中的值将变得过时。 There is an eslint rule that checks for this issue (the linked discussion also contains more details): https://github.com/facebook/react/issues/14920 . 有一个eslint规则可以检查此问题(链接的讨论中也包含更多详细信息): https : //github.com/facebook/react/issues/14920

You are correct in that useCallback is used to memoize a function. useCallbackuseCallback用于useCallback功能。 You can think of a and b (or anything used in the second argument of useCallback ) as the keys to the memoized function. 您可以将ab (或useCallback的第二个参数中使用的任何东西)视为备注函数的键。 When either a or b change, a new function is created. ab更改时,将创建一个新功能。

This is useful especially when you want something to be called on an onClick that requires some values from your component's props. 这在您希望在onClick上调用某些需要您组件的props值的东西时特别有用。

Similar to your example instead of creating a new function on every render: 与您的示例类似,而不是在每个渲染器上创建新函数:

const Signup = ({ email, onSignup }) => {
  return <button onClick={() => onSignup(email) } />;
}

you would use useCallback : 您将使用useCallback

const Signup = ({ email, onSignup }) => {
  const onClick = useCallback(() => onSignup(email), [email, onSignup]);
  return <button onClick={onClick} />;
}

This will ensure that a new function is created and passed to onClick only if email or onSignup change. 这将确保仅当emailonSignup发生更改时,才创建新功能并将其传递给onClick

The use of parameter a, b depends on whether the function that you are trying to execute takes them from the enclosing scope or not. 参数a, b取决于您尝试执行的函数是否将它们从封闭范围中获取。

When you create a function like 当您创建类似的函数时

const signupCallback = email => {
  return console.log(`sign up called with email ${email}`);
};

const memoizedsignupCallback = useCallback(() => {
  signupCallback();
}, []);

In the above case memoizedsignupCallback is created on initial render and it will have access to the values from the enclosing closure when it is created. 在上述情况下, memoizedsignupCallback是在初始渲染时创建的,并且在创建时将可以从封闭的闭包中访问值。 Not if you want to access a value that lies within its closure but can update due to some interaction, you need to recreate the memoized callback and hence you would pass in the arguments to useCallback. 如果您想访问位于其闭包内但由于某些交互作用而可以更新的值,则不是这样,您需要重新创建已记忆的回调,因此将参数传递给useCallback。

However in your case the value that memoizedsignupCallback uses is passed on by the caller while executing the method and hence it would work correctly 但是,在您的情况下, memoizedsignupCallback使用的值在执行方法时由调用方传递,因此它将正常工作

DEMO DEMO

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

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