I have a React app which fetches Github repositories with Github search API, the function looks like this;
const searchEverything = (input: string) => {
if (input.length > 2) {
setSearchPageStatus(true);
fetch(`https://api.github.com/search/repositories?q=${input}`)
.then((res) => res.json())
.then((data) => {
setRepoCount(data.total_count);
setRepos(data.items);
});
fetch(`https://api.github.com/search/users?q=${input}`)
.then((res) => res.json())
.then((data) => {
setUserCount(data.total_count);
setUsers(data.items);
});
} else {
setSearchPageStatus(false);
}
};
So I get items with "onChange" attribute like this;
<input onChange={(e) => searchEverything(e.target.value)}
type="search"
className="header__search__input"
placeholder="Search..."
/>
I am not getting errors or anything like that. But as you can see on each key stroke there is an API call and I feel like it's not efficent and if you have a long word there might be an error 403. So is there any better way to use this API ? The solution I thought is just putting a button and searching after click but it just takes away all the dynamism.