lodash debounce错误:未接收的TypeError:期望功能

发布于 2025-01-22 22:13:29 字数 576 浏览 4 评论 0原文

我正在尝试使用debounce函数,以避免打字并在表中搜索某些内容时进行多个调用。这就是我所做的 -

onChange={(_event, searchText) => React.useCallback(debounce(void setSearchText(searchText), _searchDebounceWaitTime), [])}

它给出了一个错误,说明未访问的TypeError:预期函数 我在做什么错?我在网上发现的大多数解决方案都做了类似的事情。

注意:我也尝试做 -

onChange={(_event, searchText) => React.useCallback(debounce(searchText => setSearchText(searchText), _searchDebounceWaitTime), [])}

但是这次我收到以下错误 - 无效的挂钩调用。钩子只能在功能组件的主体内部调用。这可能是出于以下原因之一:

pls帮助。

I am trying to use the debounce function to avoid multiple calls when I'm typing and searching something in a table. This is what I have done -

onChange={(_event, searchText) => React.useCallback(debounce(void setSearchText(searchText), _searchDebounceWaitTime), [])}

It gives an error stating Uncaught TypeError: Expected a function
What am I doing wrong ? Most solutions I found online have done a similar thing.

Note: I also tried doing -

onChange={(_event, searchText) => React.useCallback(debounce(searchText => setSearchText(searchText), _searchDebounceWaitTime), [])}

but this time I got the following error - Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

Pls help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奶茶白久 2025-01-29 22:13:29

首先,正如错误状态所示,React钩子不能在JSX中。应该是这样的:

const onChange = React.useCallback((_event, searchText) => setSearchText(searchText), [])

//...
return (
  <Component
    //...
    onChange={onChange}
  />
);

然后,为了进行审问,您要寻找的是一个记忆的功能:

const onChange = React.useMemo(() => debounce((_event, searchText) => setSearchText(searchText), _searchDebounceWaitTime), [])

First of all, as the error states, a react hook cannot be in the jsx. It should be like this:

const onChange = React.useCallback((_event, searchText) => setSearchText(searchText), [])

//...
return (
  <Component
    //...
    onChange={onChange}
  />
);

Then, for a debounce, what you are looking for is a memoized function:

const onChange = React.useMemo(() => debounce((_event, searchText) => setSearchText(searchText), _searchDebounceWaitTime), [])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文