SVG images scale without losing sharpness. On the web, you can style them with CSS, and their small file sizes can improve site performance. React Native can also display SVGs. This guide shows how to use them in an Expo project.
Use the Svg component to render the copied SVG. The Svg component comes from react-native-svg. Convert the markup to React Native by placing the Circle and Line components inside Svg, as shown below.
You can also use the React SVGR playgroundโ to convert SVG images to React components. Paste your SVG markup and the playground generates the component for you. Here's what it produces for the example above.
The result looks a little different from the manual conversion. In particular, it uses SvgProps so you can pass props through. That's why I recommend using the playground when converting an SVG to a component.
Converting each SVG manually can get tedious, especially when you have to change file extensions. The react-native-svg-transformer library converts SVG files into components automatically.
Add the configuration to metro.config.js in the project root. A plain React Native project might not have a metro.config.js file yet. If it doesn't, create metro.config.js in the root and add the configuration below. Include the code that sets module.exports.
If you use TypeScript, add the following declaration to declarations.d.ts so TypeScript recognizes SVG files as modules. If the file doesn't exist, create declarations.d.ts in the project root.
declarations.d.ts
declare module '*.svg' { import React from 'react'; import { SvgProps } from 'react-native-svg'; const content: React.FC<SvgProps>; export default content;}
Style an SVG by passing attributes such as fill, stroke, and strokeWidth as props. In this example, the SVG's fill attribute takes the fill prop, and its stroke attribute takes the stroke prop. Use โcurrentColorโ when you want to supply a variable color.