首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React Typescript和useCallback

React Typescript和useCallback
EN

Stack Overflow用户
提问于 2020-05-28 17:22:01
回答 1查看 2.5K关注 0票数 3

我正在尝试使用Typescript和useCallback,但是我遇到了这个错误

Expected 0 arguments, but got 1

这是我的代码

代码语言:javascript
复制
const searchFriends = useCallback(() => (filterValue: string): void => {
    if (dataState.length <= INITIAL_FRIENDS_API_REQUEST) fetchAllFriends()
    const filterValueInsensitive = filterValue.toLowerCase()
    const filtered = dataState.filter((friend: Friends) => {
      return friend.displayName?.toLowerCase().includes(filterValueInsensitive)
    }) 
    setFriendsDisplayState(filtered)
  }, [dataState,fetchAllFriends]) 

  const handleChangeSearchInput = (
    e: React.ChangeEvent<HTMLInputElement>
  ): void => searchFriends(e.currentTarget.value) // ERROR here "Expected 0 arguments, but got 1"

你知道我怎么才能修复它吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-28 17:24:21

您正在使用像useMemo's这样的useCallback语法。它不需要currying函数。正确的写法是这样的

代码语言:javascript
复制
const searchFriends = useCallback((filterValue: string): void => {
    if (dataState.length <= INITIAL_FRIENDS_API_REQUEST) fetchAllFriends()
    const filterValueInsensitive = filterValue.toLowerCase()
    const filtered = dataState.filter((friend: Friends) => {
      return friend.displayName?.toLowerCase().includes(filterValueInsensitive)
    }) 
    setFriendsDisplayState(filtered)
  }, [dataState,fetchAllFriends]) 
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62061218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档