React Typescript and useCallback



我正在尝试使用Typescript并使用回调,但是我遇到了此错误

Expected 0 arguments, but got 1

这是我的代码

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"

知道我该怎么解决吗?

您正在使用useCallback语法,例如useMemo's。它不需要咖喱功能。正确的写法是这样的

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]) 

相关内容

  • 没有找到相关文章

最新更新