PacktPublishing / Full-Stack-Development-with-Spring-Boot-3-and-React-Fourth-Edition

89 stars 70 forks source link

unreachable condition in the CarList component #9

Closed Zhaoyu-Qu closed 7 months ago

Zhaoyu-Qu commented 8 months ago

In the React frontend in the CarList.tsx file, we have the following code ... const { data, error, isSuccess } = useQuery({ queryKey: ["cars"], queryFn: getCars }); ... if (!isSuccess) { return Loading... } else if (error) { return Error when fetching cars... } else { ...

However, the else if (error) condition is unreachable, because the (!isSuccess) condition is always met first in the event of errors, effectively shadowing the error condition.

I suggest the following changes:

... const { data, isSuccess, isError, isLoading } = useQuery({ queryKey: ["cars"], queryFn: getCars }); ... if (isLoading) { return Loading... } else if (isError) { return Error when fetching cars... } else if (isSuccess) { ...

juhahinkula commented 7 months ago

Yes, thanks for pointing this out. I will fix the code.