You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TL;DR is a way for a custom hook that depends two SWR calls and have the last one take into account the response form the first one (except data and mutate) without breaking dependency collection features, deduplication or caching?
Consider this fictional scenario: I have a hook called useGetBook() however to get the book i need to call a async function getBooks on an author so i have to pass an authorIdand a bookId.
constuseGetBook(authorId: string,bookId:string)=()=>{constauthor=useGetAuthor(authorId)// Another SWR call returning an object containing an async function `getBook`constfetcher=async()=>{returnauthor.data?.getBook(bookId)}constbook=useSWRImmutable(authorId+bookId,fetcher)// Now this does not take into account `author.isLoading` returnbook}
In the code example above i could read the isLoading from author and then return a newly constructed object that returns isLoading: author.isLoading || book.isLoading but then i would forcefully cause a re-render by accessing author.isLoading. Another way i could see doing this is to move the async operations of useGetAuthor into fetcher but then i loose caching and deduplication.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
TL;DR is a way for a custom hook that depends two SWR calls and have the last one take into account the response form the first one (except
data
andmutate
) without breaking dependency collection features, deduplication or caching?Consider this fictional scenario: I have a hook called
useGetBook()
however to get the book i need to call a async functiongetBooks
on anauthor
so i have to pass anauthorId
and abookId
.In the code example above i could read the
isLoading
fromauthor
and then return a newly constructed object that returnsisLoading: author.isLoading || book.isLoading
but then i would forcefully cause a re-render by accessingauthor.isLoading
. Another way i could see doing this is to move the async operations ofuseGetAuthor
intofetcher
but then i loose caching and deduplication.Beta Was this translation helpful? Give feedback.
All reactions