Skip to content

Instantly share code, notes, and snippets.

@good-idea
good-idea / 0-readme.md
Last active March 29, 2023 21:00
SWR Lazy Hook

A simple hook to create a "lazy request" hook using swr.

! This isn't perfect !

The problem with the example below is that you need to know your variables when you call useLazyRequest. This might not work in all situations, for instance, if your component is getting the variables from state that might change. This could be fixed by putting the call to useLazyRequest within a child component that receives those variables as props.

See the typescript example below.

Not fully tested, I just wrote this on the fly. If you have any suggestions, please add them in a comment!