[英]To render auto populated search result Debounce is not working in React and i am not able to call my debounce inside useEffect
Please let me know where i am doing mistake as i am new to React and i am implementing the concepts for the first time in the live project请让我知道我在哪里做错了,因为我是 React 的新手并且我是第一次在实时项目中实现这些概念
This is place where data is getting processed
这是处理数据的地方
There are multiple notes I have about your code:关于您的代码,我有多个注释:
const inputHasChanged = useMemo(() => debounce(rxCometGetFindUsers, 500), [inputValue]);
const inputHasChanged = useMemo(() => debounce(rxCometGetFindUsers, 500), [inputValue]);
So to get this to work you do:因此,要使其正常工作,您需要执行以下操作:
I have not used "Autocomplete" before so I'm not sure what options you must include.我之前没有使用过“自动完成”,所以我不确定您必须包含哪些选项。
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
/>
)}
This is fine这可以
const handleChange = event => {
event.preventDefault();
console.log("User has entered" +event.target.value);
setInputValue(event.target.value);
};
Next, you could use debounce
function from lodash but the problem is that you probably have an async function, since you fetch something.接下来,你可以使用
debounce
功能从lodash但问题是,你可能有一个异步功能,因为你取东西。 Debounce from lodash is not able to handle this behaviour.从 lodash 去抖动无法处理这种行为。 So you need to use a p-debounce .
所以你需要使用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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.