Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native local images load slowly

I'm building an app using React Native with Expo. Everything works fine except the images, they load correctly but it takes about 2 seconds to load, and it's weird because all the images are local images and they are light too, so they should load instantly. This problem occurs also after I build and get the .apk on my android device so the images are always stored locally.

This is a portion of the main page of my app:

...
   <View style={styles.bottomItem}>
     <View style={styles.bottomItemInnerFirst}>
       <TouchableOpacity onPress={() => this.props.navigation.navigate('SecondPage')}>
         <Image
           source={require('../assets/images/iconT.jpg')}
           style={{width: '100%', height: '100%'}}
           resizeMode='contain'
         />
       </TouchableOpacity>
     </View>
   </View>
...

The problem occurs not only with the Image tag but also with ImageBackground. I've looked at this doc too https://docs.expo.io/versions/latest/guides/preloading-and-caching-assets/ but I'm using local images and I don't know what to do to make it work.

like image 208
Simone Romani Avatar asked Dec 16 '19 11:12

Simone Romani


People also ask

How do I load local images faster in react-native?

React Native FastImage is a quick way to load images in React Native. All loaded pictures are aggressively cached by FastImage. You may add your own auth headers and preload pictures to your requests. GIF caching is also supported by react-native-fast-image.

Are react-native apps slow?

React Native application are really slow most of the times. Large sets of the list, images, assets, API responses and multiple rendering, profiling, memorization, lazy-loading, I was thrown this many terms during the process of improving our application performance.

How do I zoom an image in react-native?

A view component for react-native with pinch to zoom, tap to move and double tap to zoom capability. You can zoom everything, from normal images, text and more complex nested views.


2 Answers

you can install new dependencies

npm install react-native-fast-image and change Image into FastImage

for reference, you can visit this website:- https://www.npmjs.com/package/react-native-fast-image

this dependency will increase the loading of your Image

like image 183
Bhojani Asgar Avatar answered Oct 13 '22 23:10

Bhojani Asgar


Asset Optimisation can also help you in expo. You can try the below commands to optimize assets in your project.

npm install -g sharp-cli
npx expo-optimize

Thanks!

like image 44
Vikas chhabra Avatar answered Oct 14 '22 00:10

Vikas chhabra