-
Notifications
You must be signed in to change notification settings - Fork 352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache persistence based on AsyncStorage #265
base: main
Are you sure you want to change the base?
Cache persistence based on AsyncStorage #265
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/vercel/swr-site/CRq5E9B7GBeTGUUzB5hEn9UJi86q |
5aab9c8
to
4c214c4
Compare
4c214c4
to
fe14c0f
Compare
fe14c0f
to
b025124
Compare
b025124
to
714e497
Compare
714e497
to
98e6571
Compare
const useAsyncStorage = () => { | ||
const [cache, setCache] = useState(null) | ||
|
||
useLayoutEffect(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example looks good, just 1 question: is there any reason use useLayoutEffect
here instead of useEffect
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, you're right
Thanks for the feedback, I'm gonna update this one @huozhi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not work. Cache initialization should happen inside the provider
function other than outside, because SWR creates the cache instance on initial render.
Also, a more reliable way to write back to the persistent storage is to register an onbeforeunload
listener (or similar method in RN), instead of doing it on unmount.
I do not understand why you say it will not work, if the SWR instance is only generated once the cache is initialized, as long as it is not initialized SWRConfig will never be rendered. Also remember that AsyncStorage is always returns a promise, and the provider function does not allow it to be a promise It would be great if you have the opportunity to test this workaround, because on my side it works without any problem |
98e6571
to
553ab12
Compare
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
|
This PR adds a new approach for cache persistence based on AsyncStorage in React Native