Skip to content

Instantly share code, notes, and snippets.

@jrdn91
Last active July 10, 2023 15:59
Show Gist options
  • Save jrdn91/26482e77a8c972eacde72a0e78108dac to your computer and use it in GitHub Desktop.
Save jrdn91/26482e77a8c972eacde72a0e78108dac to your computer and use it in GitHub Desktop.
Search results useQuery example
const [search, setSearch] = useState("")
const debouncedSearch = useDebounce(search, 400)
const [searchResults, setSearchResults] = useState<Agent[]>([])
const { data: agents, isLoading } = useListAgents({
search: debouncedSearch,
})
useEffect(() => {
setSearchResults(() => agents || [])
}, [agents, search])
return (
<AllAgentsTable
editAgent={editAgent}
searchResults={searchResults}
/>
)
const [search, setSearch] = useState("")
const debouncedSearch = useDebounce(search, 400)
const { data: agents, isLoading } = useListAgents({
search: debouncedSearch,
})
return (
<AllAgentsTable
editAgent={editAgent}
agents={agents}
/>
)
import queryKeys from "@/api/queryKeys"
import { useQuery } from "@tanstack/react-query"
import AgentsService from "@/api/AgentsService"
import querifyFilters from "@/utils/querifyFilters"
import { ListAgentsResponse } from "shared/types/Agent"
const listAgents = (data: { search: string }) =>
AgentsService.get<ListAgentsResponse>(`/${querifyFilters(data)}`).then(
(res) => res?.data?.data?.agents
)
function useListAgents(data: { search: string }) {
return useQuery({
queryKey: queryKeys.agent.list(data).queryKey,
queryFn: () => listAgents(data),
})
}
export default useListAgents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment