admin管理员组文章数量:1208155
I have a search input, to make API calls on the fly. I'd like to implement debounce to reduce the amount of server calls.
_debouncedSearch() {
debounce(this.props.fetchRoutes(this.state.searchText), 1000);
}
_updateResults(searchText) {
this.setState({searchText});
this._debouncedSearch();
}
I am expecting debouncedSearch
every 1 second. But it is still called on the fly. And throw errors:
Uncaught TypeError: Expected a function at debounce (lodash.js?3387:10334)
Uncaught Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development.
I feel like this question must get asked around a lot, but none of the solution seems to work for me. Could someone explain to me what exactly is the problem here? I thought debounce is just a setTimeOut.
Thanks
I have a search input, to make API calls on the fly. I'd like to implement debounce to reduce the amount of server calls.
_debouncedSearch() {
debounce(this.props.fetchRoutes(this.state.searchText), 1000);
}
_updateResults(searchText) {
this.setState({searchText});
this._debouncedSearch();
}
I am expecting debouncedSearch
every 1 second. But it is still called on the fly. And throw errors:
Uncaught TypeError: Expected a function at debounce (lodash.js?3387:10334)
Uncaught Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development.
I feel like this question must get asked around a lot, but none of the solution seems to work for me. Could someone explain to me what exactly is the problem here? I thought debounce is just a setTimeOut.
Thanks
Share Improve this question asked Dec 1, 2017 at 18:39 leogoesgerleogoesger 3,8307 gold badges38 silver badges74 bronze badges 2 |3 Answers
Reset to default 10constructor(props) {
super(props);
this.state = {
searchText: '',
};
this._debouncedSearch = debounce(
() => this.props.fetchRoutes(this.state.searchText),
1000
);
}
_updateResults(searchText) {
this.setState({searchText});
this._debouncedSearch();
}
Here is the fullworking code in case someone needs it!
_.debounce
is already a carried out function (function returns function ) . Then _debouncedSearch
should be an attribute of the class , and not method :
_debouncedSearch= debounce(() => this.props.fetchRoutes(this.state.searchText), 1000);
instead of :
_debouncedSearch() {
debounce(this.props.fetchRoutes(this.state.searchText), 1000);
}
Also, notice , the first argument of _.debounce
is a function (() => this.props.fetchRoutes...
) , not directly this.props.fetchRoutes...
Recently found this issue helpful. Here's my optimized solution:
const [searchTerm, setSearchTerm] = useState("");
const [result, setResult] = useState([]);
async function fetchData(searchTerm: string) {
const { data } = await client.query(searchTerm);
setResult(data);
}
const debounceLoadData = useMemo(() => debounce(fetchData, 700), []);
useEffect(() => {
window.addEventListener("keydown", debounceLoadData(searchTerm));
return () => {
window.removeEventListener("keydown", debounceLoadData(searchTerm));
};
}, [searchTerm]);
This will only call the fetchData()
function once after the user stops typing for 700ms. Then I use useMemo
to cache every result. If the user searches the same value multiple times it will return the cached value and not invoke the fetchData()
function more than necessary.
本文标签: javascriptusing debounce for search input in reactStack Overflow
版权声明:本文标题:javascript - using debounce for search input in react - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738692403a2107179.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
this.props.fetchRoutes(this.state.searchText)
probably returns void or something that is not a function, try to debounce a function instead. Using either() =>this.props.fetchRoutes(this.state.searchText)
orfunction() { this.props.fetchRoutes(this.state.searchText) }
P.S. : debounce is not just a setTimeout – skornous Commented Dec 1, 2017 at 18:44