ideally, filtering should happen in the backend. That way, you can just make the name of the filtered movie part of your query key. React Query will then automatically refetch if the key changes, and also cache each entry filtering individually:
const [name, setName] = React.useState()
const { data } = useQuery(['movies', name], () => fetchMovies(name)))
if you don't do the filtering in the backend, you can just run the filtering on the data returned from useQuery:
const [name, setName] = React.useState()
const { data } = useQuery(['movies'], () => fetchMovies()))
const result = filterByName(data, name)
this would go well into a custom hook.
if referential stability is needed, you can wrap the result creation in useMemo:
const result = React.useMemo(() => filterByName(data, name), [data, name])
You can also leverage the select option of react-query:
const [name, setName] = React.useState()
const { data } = useQuery(
['movies'],
() => fetchMovies()),
{
select: (response) => filterByName(response, name)
}
)
in this case, the data returned from useQuery will already be filtered, and you don't have to worry about memoization.