I use svg in my react-native app next way:
import TestLogo from "../../assets/TestLogo.svg";
<TestLogo />
I use background image in next way:
import background from '../../assets/modules/auth/signUpPattern.png'
<ImageBackground source={background} style={{width: '100%', height: '100%', alignItems: 'center'}}>
If I use the next syntax:
import Background from '../../assets/modules/auth/signUpPattern.svg'
<ImageBackground source={<Background />} style={{width: '100%', height: '100%', alignItems: 'center'}}>
I got empty space, not svg image background, how to fix it?
The SVG image format can be used as a background image in the same way as the PNG, JPEG, and GIF image format.
SVG images can be used as background-image in CSS as well, just like PNG, JPG, or GIF. All the same awesomeness of SVG comes along for the ride, like flexibility while retaining sharpness. Plus you can do anything a raster graphic can do, like repeat.
Style: import React from 'react-native'; let { StyleSheet } = React; let styles = StyleSheet. create({ backgroundImage: { flex: 1, resizeMode: 'cover', // or 'stretch' } }); I'm pretty sure you can get rid of the <View> wrapping your image and this will work.
Install react-native-svg-transformer by running this command yarn add react-native-svg-transformer --dev or using npm if you prefer npm i react-native-svg react-native-svg-transformer --save-dev. Install babel-plugin-inline-import by running yarn add babel-plugin-inline-import --dev. You need to update your metro.
import TestLogo from "../../assets/TestLogo.svg";
<TestLogo />
You can use this for adding background Image for the screen. Just need to add the style to the TestLogo component.
Example:
<TestLogo
style={{
position: 'absolute',
top: 0,
left: 0,
right: 16,
bottom: 0,
}}
/>
react-native doesn't support .svg formats. u can use library like react-native-svg or any other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With