Mad Hatter's blog in Wonderland

SWR is quite the hook

So I have been using SWR in my Next.js application. It has quite simplified the fetching of data and managing its state.

When there is a barrage of react components, fetching APIs and saving the data to the components state can get quite messy. Also, its quite difficult to communicate with the global state without using a store library. SWR has solved some of these issue for me.

One place where its quite useful is to manage the auth state for all the pages. Instead of fetching on every single page render, I have create a "useUser" hook with SWR to save the state fetched from the auth API. The same hook can be recycled in different pages and even components. You can also easily mutate the data by passing the mutate function through the hook.

So SWR is quite the "hook" indeed!