Working with SVGs in React Native

How to display and animate SVG images in React Native

Samaila Bala
StackAnatomy
8 min readApr 26, 2022

--

Scalable Vector Graphic (SVG) is an image format that uses vector graphics to display the image you see. They are now popular among developers because they scale to any resolution. Other advantages of SVGs include:

  • Easily Editable: SVG files can be created and edited with any text editor. You can also manipulate the properties with CSS.
  • Small File Size: due to the nature of SVGs, the resolutions don’t affect the file sizes allowing the file sizes to be minimal when compared to the pixel-based counterparts
  • SEO Friendly: because of its XML markup language format SVG is SEO friendly as you can add keywords and descriptions to the image directly.
  • They can be animated: We said that customizing and editing SVG files is easy. We can then animate them with CSS and JavaScript

In this article, we will be looking at different ways of manipulating SVG images in React Native applications.

Setting up a React Native Project

We will be using the Expo framework to bootstrap our React Native application. Make sure you have Node.js/npm on your machine. Open a terminal and run the code below to install expo-cli globally.

After installation, run the code below to create a new react-native project

This command will launch an interactive prompt for you to select a template. Choose blank and press the enter/return key to continue the installation.

Once the process is complete, navigate to the root of the project directory and run the code below to start the expo development server.

You will be presented with an interactive menu similar to the screenshot below.

The Expo dev server allows you to test your application locally while in development. You will need an emulator or the Expo Go App to run the app. (Note: the installation of an emulator isn’t covered in this article.) Nevertheless, you can check [how to install an emulator this article to install the required tools.

Assuming you have an emulator installed, press the relevant key that applies to the emulator, and it’ll run the app on the emulator.

Creating SVG in React Native

We will be creating a custom loader for an application. To start, we will need react-native-svg, a library that provides SVG support for react-native applications. Open a terminal and navigate to the root of your project. Run the code below to install the library

After installing, create a file called Loader.js in the root directory and paste the code below into the file.

If you’ve used SVG images on the web, this should look familiar. The major difference here is that we are using the components provided by react-native-svg to create our SVG image instead of using HTML elements. The react-native-svg components use sentence case in naming to differentiate from the HTML elements, which are lower case. To render our SVG go to the App.js file and modify it to look like the code below.

Save, and reload your emulator; you should be presented with a screenshot similar to the screenshot below.

Converting an existing SVG image to a React Native component can be a chore. Luckily, an open-source tool called SVGR Playground lets you generate a React Native component from an SVG.

It lets you paste an SVG image on the left and generates a React Native SVG component on the right to copy and use. You can play with it to see how it works.

Open Source Session Replay

OpenReplay is an open-source alternative to FullStory and LogRocket. It gives you full observability by replaying everything your users do on your app and showing how your stack behaves for every issue. OpenReplay is self-hosted for full control over your data.

Happy debugging for modern frontend teams — start monitoring your web app for free.

Adding External SVG to React Native applications

The advantage of creating a React Native SVG component is that it is customizable. We can pass props and animate, as we will see in the next section, but there are times when we just want to use an external SVG image without customizing it.

There are two ways to achieve this. The react-native-svg library has a SvgUri component that allows us to create SVG components from external sources. Create a file called SvgExternal.js and paste the code below

It provides a uri prop that we can pass a link to an external site. Go to the App.js file and modify the code to display the external SVG.

Now you can open your emulator to view the changes.

Another way of achieving this is by using another library called react-native-svg-transformer. It lets you import SVG files into React Native projects. Run the code below in your projects terminal to install the library

Create a file called metro.config.js in your project's root and paste the code below into it.

The metro.config.js is a config file for Metro, a JavaScript bundler for React Native. If you created a react-native project without Expo, paste the code below in your metro.config.js as the configurations for Expo are different from a bare React Native app.

The next step is getting an SVG file. You can download any on the internet or use this. Move the downloaded file to the assets directory in your project. Now go to the App.js file and modify it like the code below to use the file you've downloaded.

Save when you’re done, and view the results in your emulator.

Animating SVGs in React Native

We’ve so far looked at different ways of adding SVG to react-native. In this section, we will look at how to animate SVG in react-native. We plan to achieve the screenshot below.

What we want to do is to change the stroke color of the SVG component every few milliseconds. To get started, create a file called AnimatedLoader.js and paste the code below. We will be using the React Native Animated API to create the animations.

The first thing we’ve done here is to make the Path component animable, allowing us to apply animations to the Path component.

The next step is creating an Animated.Value that we can attach to an animated component. We used a useRef hook to store the Animated.Value so we don't get to mutate it directly. We've created our animation in the useEffect hook: a loop that creates a fade-in and fade-out effect.

The last thing to do is hook up the animation with the component that needs it. In this case, the AnimatedPath. We've attached the color Animated.Value to the stroke prop of the AnimatedPath to interpolate the color Animated Value, creating a color mapping between the inputRange and outputRange. Save and reload your emulator to view the changes.

Conclusion

We’ve covered how to use and animate SVG’s in React Native. When it comes to animating, there is much more, going beyond the scope of this article. Endeavor to check the React Native Animated API. Animating SVG in React Native has some performance drawbacks, especially for complex animations, so endeavor to keep it simple. The code of this article is available here.

Originally published at https://blog.openreplay.com on April 26, 2022.

--

--