'useEffect debounce search technique

i'm trying to implement search functionality but i don't want to call the api every time i type something . here is the code:

const [term, setTerm] = useState("");
const [result, setResult] = useState([]);

useEffect(() => {
  const search = async () => {
    const respond = await axios.get("https://en.wikipedia.org/w/api.php", {
      params: {
        action: "query",
        list: "search",
        origin: "*",
        format: "json",
        srsearch: term,
      },
    });
    setResult(respond.data.query.search);
  };

  if (!result.length) {
    if (term) {
      search();
    }
  }
}, [term, result.length]);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source