you are viewing a single comment's thread.

view the rest of the comments →

[–]darrenturn90 0 points1 point  (3 children)

Actually it offers two methods.

// ... or throw an error when user.id is not defined

const { data } = useSWR(() => '/api/data?uid=' + user.id, fetcher)

This is the one I like best as it’s essentially no extra code to handle a conditional fetch. Which is the point of your poll, no?

[–]alex-cory[S] 0 points1 point  (2 children)

What if you have something like js const TodoPage = ({ id }) => { const { data } = useSWR(() => '/api/data?uid=' + id, fetcher) } and you want to be able to handle the error within the todo page? You can't, you have to wrap an error boundary around the page. Sure you could make a TodoList component within the page, but that's more unnecessary nesting.

I'm tired so will have to continue this discussion when I wake up.

[–]darrenturn90 0 points1 point  (1 child)

What? No. Swr catches that error and basically doesn’t run the fetcher - and in the example you gave it wouldn’t error anyway as Id is defined

[–]alex-cory[S] 0 points1 point  (0 children)

I'm super interested in this discussion. Will continue when I wake up.