React Typescript and useCallback
Asked Answered
P

1

5

I'm trying to use Typescript and useCallback, but I'm having this error

Expected 0 arguments, but got 1

This is my code

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"

Any idea how can I fix it?

Photodrama answered 28/5, 2020 at 9:22 Comment(0)
B
7

You are using useCallback syntax like useMemo's. It doesn't need a currying function. The correct way to write it is like

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]) 
Bowls answered 28/5, 2020 at 9:24 Comment(1)
Happy to have helped :-)Bowls

© 2022 - 2025 — McMap. All rights reserved.