State Management · Senior · Question 38 of 75
How do you pass server-fetched data to SWR so the first client render has it?
- ✓Correct answer: Provide it as
fallbackin<SWRConfig>keyed by the same key (orfallbackDataper hook) - 2Write it into
localStorageon the server - 3Call
mutateinuseEffectafter mount
Explanation
<SWRConfig value={{ fallback: { '/api/user': user } }}> makes useSWR('/api/user') return data immediately; SWR still revalidates according to its options.