I'm following a React-hooks tutorial and I don't understand why does the function expression, timerId in the top useEffect is being invoked automatically, but the function expression, search, in the bottom useEffect requires it to be called search() in order to invoke it?
useEffect(() => {
const timerId = setTimeout(() => {
setDebouncedTerm(term);
}, 1500);
return () => {
clearTimeout(timerId);
};
}, [term]);
useEffect (() => {
const search = async() => {
const { data } = await axios.get('https://en.wikipedia.org/w/api.php', {
params: {
action: 'query',
list: 'search',
origin: '*',
format: 'json',
srsearch: debouncedTerm
}
});
setResults(data.query.search);
};
search();
}, [debouncedTerm]);