简体   繁体   English

渲染自动填充的搜索结果 Debounce 在 React 中不起作用,我无法在 useEffect 中调用我的 debounce

[英]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:关于您的代码,我有多个注释:

  1. Use either debounce or throttle.使用去抖或节流。 But don't use them together.但不要同时使用它们。 You can find a very good explanation of both techniques here .您可以在此处找到对这两种技术的非常好的解释。
  2. Remove your dependancies from useEffect.从 useEffect 中删除您的依赖项。 In your case useEffect will execute every time input changes + every time the fetch is set.在您的情况下, useEffect 将在每次输入更改 + 每次设置提取时执行。 From this code that you posted there is actually no point of you having useEffect.从您发布的这段代码来看,实际上您没有使用 useEffect 的意义。 Render can be handled by useMemo.渲染可以由 useMemo 处理。
  3. useMemo must have its own dependancy array. useMemo 必须有自己的依赖数组。 In your case its executing on every render.在您的情况下,它在每次渲染时执行。 So you need to move its dependancies from useEffect to useMemo.所以你需要将它的依赖从 useEffect 移动到 useMemo。 For example you could do 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM