Home > database >  how do i prevent typescript from complaining in this useEffect hook
how do i prevent typescript from complaining in this useEffect hook

Time:02-11

i am trying to use this useEffect hook but cant fix the type constraint of typescript

useEffect(() => {
        const {
            isFetching: isLoading,
            isFetchingNextPage: loadingMore,
            fetchNextPage,
            hasNextPage,
            data,
            error,
        } = useProductsQuery({ limit: 20, pageParam: 1, ...query });
        if (error) return <p>{error.message}</p>;
    }, [])

CodePudding user response:

There is no need to use an effect at all:

 const {
            isFetching: isLoading,
            isFetchingNextPage: loadingMore,
            fetchNextPage,
            hasNextPage,
            data,
            error,
        } = useProductsQuery({ limit: 20, pageParam: 1, ...query });

...

if (error)  {
    return <p>{error.message}</p>
}

return <... non error elements />

  • Related