Swr: [React Native] Can't find variable: __dirname

Created on 20 Nov 2019  路  6Comments  路  Source: vercel/swr

I simply added

import useSWR from 'swr'

then error occurred Can't find variable: __dirname
__dirname is on compiled dist code, but React Native doesn't support _dirname.

help wanted react-native

Most helpful comment

The current cache structure needs some refactoring to support async cache mechanisms. And after that, we will be able to support custom cache APIs like AsyncStorage.

All 6 comments

The __dirname variable is located in node_modules/swr/dist/index.js
I need to import swr/esm to bypass it

@quietshu I can help take a look on this one :)

I am facing a similar issue but when importing it from import useSWR from '@zeit/swr' it works fine @n3tr Thanks for maintaining this. Do you have any React native example. Because I am facing a hard time getting the cached data when the device is offline.

I am expecting this library to serve the most recent cached api response and when online it revalidates and bring the recent response. Does is it work this way or I am missing something. Thanks again @n3tr .

@mohmdalfaha doesn't it return a cached data when device is offine? 馃

I only use swr in my hobby project and haven't thought about the online/offline thing 馃槄 .

I will help take a look when I have time. My idea is just creating custom fetch hook, observing netinfo event ,and revalidate if needed.

The current cache structure needs some refactoring to support async cache mechanisms. And after that, we will be able to support custom cache APIs like AsyncStorage.

@quietshu I really liked the amazing hook you built, Although I am wondering if it supports offline mode, I have tried it with offline mode but it did not work

Was this page helpful?
0 / 5 - 0 ratings