Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use svg image as background image in react-native?

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?

like image 896
befreeforlife Avatar asked Feb 18 '20 12:02

befreeforlife


People also ask

Can I use SVG as background image react-native?

The SVG image format can be used as a background image in the same way as the PNG, JPEG, and GIF image format.

Can an SVG be a background image?

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.

How do I fit a background image in react-native?

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.

How do I integrate SVG in react-native?

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.


2 Answers

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,
   }}
 />
like image 192
Sharad S Katre Avatar answered Nov 10 '22 04:11

Sharad S Katre


react-native doesn't support .svg formats. u can use library like react-native-svg or any other.

like image 33
Waleed Azhar Avatar answered Nov 10 '22 03:11

Waleed Azhar