【发布时间】:2021-09-08 17:23:08
【问题描述】:
请让我知道我在哪里做错了,因为我是 React 新手,而且我是第一次在现场项目中实现这些概念
这是处理数据的地方
【问题讨论】:
标签: javascript reactjs react-native react-hooks debouncing
请让我知道我在哪里做错了,因为我是 React 新手,而且我是第一次在现场项目中实现这些概念
这是处理数据的地方
【问题讨论】:
标签: javascript reactjs react-native react-hooks debouncing
我对您的代码有多个注释:
const inputHasChanged = useMemo(() => debounce(rxCometGetFindUsers, 500), [inputValue]);
所以要让这个工作你做:
我之前没有使用过“自动完成”,所以我不确定您必须包含哪些选项。
return (
<Autocomplete
id="find-user"
className="autoCompSearch"
style={{ width: 370 }}
getOptionLabel={option => getValues(option)}
defaultValue={defaultObj || ""}
filterOptions={x => x}
options={options}
autoComplete
includeInputInList
freeSolo
disableOpenOnFocus
renderInput={params => (
<TextField
{...params}
placeholder="GLOBAL SEARCH"
fullWidth
onChange={handleChange}
onKeyDown={onKeyDownHandler} // I would remove this, dont see a reason why you would need this and onChange
/>
)}
这很好
const handleChange = event => {
event.preventDefault();
console.log("User has entered" +event.target.value);
setInputValue(event.target.value);
};
接下来,你可以使用lodash 中的debounce 函数,但问题是你可能有一个异步函数,因为你获取了一些东西。 Debounce from lodash 无法处理这种行为。
所以你需要使用p-debounce。
import pDebounce from 'p-debounce';
const inputHasChanged = useMemo(() => pDebounce(rxCometGetFindUsers, 500), [inputValue]);
const rxCometGetFindUsers = async = () =>{
let newData = await ... //do your fetching
setOptions(newData)
}
【讨论】: