SentryError: Native Client is not available, can’t start on native when updating expo-cli to 4.x.x (from version 3.22.3)

TL;DR: Update your metro.config.js to use @expo/metro-config based on the latest guidelines (SDK 40+) –

It’s funny when you encounter an error in a project, and after spending a lot of effort researching it, to find out that the cause is the same with a totally different error on a very different project.

In my case this was caused by outdated metro.config.js file, specifically the SVG loading code that uses react-native-svg-transformer.

To fix it, I replaced the metro config with the following:

const { getDefaultConfig } = require("@expo/metro-config");

module.exports = (async () => {
  const {
    resolver: { sourceExts, assetExts }
  } = await getDefaultConfig(__dirname);
  return {
    transformer: {
      babelTransformerPath: require.resolve("react-native-svg-transformer")
    },
    resolver: {
      assetExts: assetExts.filter(ext => ext !== "svg"),
      sourceExts: [...sourceExts, "svg"]
    }
  };
})();

And also intall the '@expo/metro-config' module:

yarn add @expo/metro-config
// or
npm install @expo/metro-config

More info on the other error – SVG Icons Not Loaded After Updating ReactNative Expo to Version 40

Cheers!

Icons Not Loaded After Updating ReactNative Expo to Version 40

TL; DR: If you’re loading SVGs, check into your metro.config.js and see if you’re using the getDefaultConfig from '@expo/metro-config'. If you’re requiring it from 'metro-config', you should update your code based on the one below. More info in the readme ofreact-native-svg-transformer

Recently I’ve had an issue with updating a project I was working on – after updating the Expo SDK to version 40, the icons stopped working.

The project was using react-native-elements, so this was my first guess for the cause of the issue. Digging a bit deeper, it turned out that any icon from @expo/vector-icons was shown as an X in a square.

Digging through the project (and github issues) I decided to create an empty expo project and gradually include the files. Doing so, I found out that there was a custom metro.config.js that took care for loading the SVGs. Looking into the readme of the 'react-native-svg-transformer' and Eurica 🙂 from version 40 or newer, the code in the metro.config.js should be different:

const { getDefaultConfig } = require("@expo/metro-config");

module.exports = (async () => {
  const {
    resolver: { sourceExts, assetExts }
  } = await getDefaultConfig(__dirname);
  return {
    transformer: {
      babelTransformerPath: require.resolve("react-native-svg-transformer")
    },
    resolver: {
      assetExts: assetExts.filter(ext => ext !== "svg"),
      sourceExts: [...sourceExts, "svg"]
    }
  };
})();

And don’t forget to install the '@expo/metro-config' module:

yarn add @expo/metro-config
// or
npm install @expo/metro-config

That’s it and happy hacking 🙂

Messing with Expo Permissions caused ImagePicker to misbehave

Have you seen the following alert?

Sorry, we need media library permissions to make this work!

Well.. me too! 🙂 And I’ve seen this error despite the fact that I’ve had both the "CAMERA" and "MEDIA_LIBRARY" permissions added to my android.permissions array.

Googling didn’t help much, because it just led me to either the expo permissions docs or to the ImagePicker docs, from which it seemed that the needed permissions are already in.

Luckily, I started to dig through the expo GitHub issues, and found an issue by folks facing the same problem.

The solution is to add the “READ_EXTERNAL_STORAGE” and the “WRITE_EXTERNAL_STORAGE” to the permissions array, despite the docs listing them as ones that are added by default.

Updating a React Native/Expo image file does not update the visualization of this image everywhere in the app

I’ve had an interesting problem when saving and updating images in a React Native application built with Expo.

I’m building an app that has contacts and images (that are either taken from the phone contact entry or picked from the gallery).

The issue was that editing the image at one place and saving it, would not update the contact image in the contacts list. When updating the image, I was updating the image file and overriding it in the filesystem.

After saving it, and going to the previous screen, the old image was still there. Only after refreshing the application it was replaced.

Since I was reusing the file name, the prop in the contact card was not modified (the file path was the same), so the component didn’t know it had to re-render.

To solve that, I decided to update my helper function to add a timestamp to the filename. This way the file path would change, forcing all the components with the image to re-render.

export async function persistCachedFile ( cachedFile: string, permanentFolder: string, fileId: string ) {
    const permanentDirectoryPath = `${ FileSystem.documentDirectory }${ permanentFolder }/`
    const uniqueFilePath = `${ permanentDirectoryPath }${ fileId }-${ Date.now() }`;

    await ensureDirExists( permanentDirectoryPath );

    await FileSystem.copyAsync( {
        from: cachedFile,
        to: uniqueFilePath
    } );

    return uniqueFilePath;
}

The downside here is, that the old files are forever going to stay in the app directory. To avoid that, we need to add a cleanup function. I came up with something the following function that runs each time we copy the file.

export async function cleanupOldFilesAsync ( folder: string, fileId: string ) {
    // Finbd all files that have the imageId in their file name (and delete then):
    const directoryFiles = await FileSystem.readDirectoryAsync( folder );
    const previousImages = directoryFiles.filter( file => {

        if ( file.includes( fileId ) ) {
            return true;
        }
        return false;
    } );

    // Delete previous images.
    if ( previousImages.length ) {
        previousImages.forEach( previousImage => {
            // We don't await, because removing the files is not critical
            deleteAsync( `${ folder }${ previousImage }` );
        } )
    }
}

Now call the cleanupOldFiles from persistCachedFile (before we store the updated file) and voilà : )

The end result is:

import {
	deleteAsync,
	getInfoAsync,
	makeDirectoryAsync,
	readDirectoryAsync,
	copyAsync,
	documentDirectory
} from 'expo-file-system';

export async function ensureDirExists ( directory: string ) {
	const dirInfo = await getInfoAsync( directory );
	if ( !dirInfo.exists ) {
		await makeDirectoryAsync( directory, { intermediates: true } );
	}
}


export async function cleanupOldFilesAsync ( folder: string, fileId: string ) {
	// Finbd all files that have the imageId in their file name (and delete then):
	const directoryFiles = await FileSystem.readDirectoryAsync( folder );
	const previousImages = directoryFiles.filter( file => {

		if ( file.includes( fileId ) ) {
			return true;
		}
		return false;
	} );

	// Delete previous images.
	if ( previousImages.length ) {
		previousImages.forEach( previousImage => {
			// We don't await, because removing the files is not critical
			deleteAsync( `${ folder }${ previousImage }` );
		} )
	}
}

export async function persistCachedFile ( cachedFile: string, permanentFolder: string, fileId: string ) {
    const permanentDirectoryPath = `${ FileSystem.documentDirectory }${ permanentFolder }/`
    const filePath = `${ permanentDirectoryPath }${ fileId }`;
    const uniqueFilePath = `${ filePath }-${ Date.now() }`;

    cleanupOldFilesAsync( permanentDirectoryPath, fileId )
    await ensureDirExists( permanentDirectoryPath );

    await FileSystem.copyAsync( {
        from: cachedFile,
        to: uniqueFilePath
    } );

    return uniqueFilePath;
}

Android Emulator Losing Internet Connectivity

Spoiler: When having Internet connectivity issues, make sure that Android Studio is running. (because it acts as a proxy for the emulator)

So I’ve had a reoccurring issue where my Android Emulator device would lose connectivity from time to time, and to fix it I went to delete it and create a new device from the Android Virtual Device Manager (AVD). Unfortunately, this was only temporary and the issue would appear again at a point.

After happening again, I decided to try to debug it instead of starting from scratch. It turned out, that in order to have an internet connection, the Android Studio should be started. The reason for losing the connectivity was because I was sometimes closing the Android Studio in order to decrease the load on my PC (I’m working with React Native and expo, and don’t use the Android Studio much).

So before trying something fancy from the SO answers to the “Android emulator not able to access the internet” question, make sure that Android Studio is running 🙂