web-base/react/shared/hooks/async-search.js

21 lines
566 B
JavaScript
Raw Normal View History

import {useEffect, useState} from "react";
export default function useAsyncSearch(callback, minLength = 1) {
const [searchString, setSearchString] = useState("");
const [results, setResults] = useState(null);
useEffect(() => {
2023-01-25 14:15:34 +01:00
if (minLength > 0 && (!searchString || searchString.length < minLength)) {
setResults([]);
return;
}
callback(searchString).then(results => {
setResults(results || null);
});
}, [searchString]);
return [searchString, setSearchString, results];
}