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!
Hello @good-idea, I was working on something similar but I needed to be compatible with Suspense. In my case I used conditional fetching to solve it.
Thanks, your idea helped me a lot, I would like to share mine with you:
https://gist.github.com/stivsk/44854eb08884cb7d876c8e549afaa343