简体   繁体   English

如何在 useCallback 钩子中传递参数?

[英]How to pass an argument in useCallback hook?

I'm trying to use the useCallback hook, to change the language using gatsby-intl plugin, they have a method ( changeLocale() ) what can be used to change the default language of the site.我正在尝试使用useCallback钩子,使用gatsby-intl插件更改语言,他们有一个方法( changeLocale() )可用于更改站点的默认语言。 I wanted to avoid passing props in JSX arrow's functions despite the solution is working so I'm trying to use the useCallback hook.尽管解决方案有效,但我想避免在 JSX 箭头的函数中传递道具,所以我尝试使用useCallback钩子。

onClick={()=>switchLanguage(language.iso)}

Here's my component:这是我的组件:

import React, { useCallback, useState } from 'react';
import { changeLocale } from 'gatsby-plugin-intl';
import { useLanguages } from '../../../hooks/useLanguages';

export const LanguageSwitcher = (callback, deps) => {
  const languages = useLanguages();

  const switchLanguage = useCallback(language => changeLocale(language),[]);

  return <ul>
    {languages.map((language, index) => <li key={index} onClick={switchLanguage(language.iso)}>{language.name}</li>)}
  </ul>;

};

The code above creates an infinite rendering, the code is entering on switchLanguage function without clicking it.上面的代码创建了一个无限渲染,代码是在没有点击它的情况下进入switchLanguage函数。

However, if I remove the argument, it works as expected but I don't know what language is the user clicking.但是,如果我删除该参数,它会按预期工作,但我不知道用户单击的是什么语言。

  const switchLanguage = useCallback(()=> console.log('item clicked'),[]);

I've also tried to use other hooks such as useState but it creates too many renders.我也尝试使用其他钩子,例如useState但它创建了太多的渲染。

How can I pass an argument to the useCallback ?如何将参数传递给useCallback If it is not possible, which will be the best workaround or approach?如果不可能,那将是最好的解决方法或方法?

onClick={switchLanguage(language.iso)} calls switchLanguage and sets its return value as onClick , just like onClick = switchLanguage(language.iso) would outside JSX. onClick={switchLanguage(language.iso)}调用switchLanguage并将其返回值设置为onClick ,就像onClick = switchLanguage(language.iso)在 JSX 之外一样。

To bind the argument to it, you'd use a wrapper function:要将参数绑定到它,您可以使用包装函数:

onClick={() => switchLanguage(language.iso)}

...or bind , though it also creates a function: ...或bind ,尽管它也创建了一个函数:

onClick={switchLanguage.bind(null, language.iso)}

But : There's probably not much to be gained by using useCallback in your example.但是:在您的示例中使用useCallback可能没有什么useCallback That being the case, you probably don't need switchLanguage at all:既然如此,您可能根本不需要switchLanguage

import React, { useCallback, useState } from 'react';
import { changeLocale } from 'gatsby-plugin-intl';
import { useLanguages } from '../../../hooks/useLanguages';

export const LanguageSwitcher = (callback, deps) => {
  const languages = useLanguages();

  return <ul>
    {languages.map((language, index) => <li key={index} onClick={() => changeLocale(language.iso)}>{language.name}</li>)}
  </ul>;
};

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

相关问题 如何正确使用 UseCallback Hook - How to properly use the UseCallback Hook 如何在 svelte 中使用 useCallback 钩子 - how do I use useCallback hook in svelte 当 useCallback 缺少依赖时 React useState 和 useCallback hook 如何工作 - How does React useState and useCallback hook work when useCallback lacks dependencies React Hook useCallback 收到一个依赖未知的函数。 改为传递内联函数 - React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead 反应挂钩useCallback没有依赖 - React hook useCallback without dependencies 当我通过时,React useCallback 会做什么。 第一个参数中的对象 - What React useCallback do when I pass. an object in first argument 如何在 useEffect/useCallback-hook 中正确处理来自 React Context 的数据 - How to correctly work with data from React Context in useEffect/useCallback-hook React Hook useCallback 收到一个 function ,其依赖关系未知。 改为传递内联 function。 使用备忘录不起作用 - React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead. useMemo doesn't work FlatList 的 renderItem 应该用 react useCallback 钩子包装吗? - Should renderItem of FlatList be wrapped with react useCallback hook? 我的 React 组件中 useCallback 挂钩的意外行为 - Unexpected behavior of useCallback hook in my React component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM