gpt4 book ai didi

reactjs - react typescript 和 useCallback

转载 作者:行者123 更新时间:2023-12-04 13:14:12 24 4
gpt4 key购买 nike

我正在尝试使用 Typescript 和 useCallback,但出现此错误
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 .它不需要柯里化(Currying)功能。正确的写法就像

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

关于reactjs - react typescript 和 useCallback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62061218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com