16

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?

6
  • Support of SVG images seems to be a feature of RN 0.61 (didn't test it, but read it here If doesn't work, or you need support in RN < 0.61, you have to use an Component for this like this Commented Feb 18, 2020 at 13:17
  • I use react-native-svg and it works. The problem only how to use it background Commented Feb 18, 2020 at 13:56
  • 1
    You can't post your Background as Component to the source. At least you can try to use data: as uri-scheme (like described here) but I doubt that this will work. I've read something that svg doesn't work within `data:'. What's about to paste it as Children and use styling to scale-up it as you need. Have a look at sourcecode of <ImageBackround> shows you, how it's implemented Commented Feb 18, 2020 at 14:19
  • Does it work with 'data:', or you added it as Children? Nice that it helps you out. Commented Feb 18, 2020 at 15:24
  • 1
    @befreeforlife hey, did you end up figuring it out? Commented Jan 29, 2022 at 16:22

3 Answers 3

12
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,
   }}
 />
Sign up to request clarification or add additional context in comments.

2 Comments

I don't think you can directly import and use a SVG graphic unless using a library like react-native-svg like @Waleed Azhar says in his reply
Yes, If you are using typescript then you need to use third-party dependencies
2

Put it inside the container, style the parent, and that should do the work.

import { View, StyleSheet } from "react-native";
import SVGShape from "../assets/icons/path";

export default ShapeSVG = () => {
  return (
    <View style={{
      position: "absolute",
      width: "100%",
      height: "32%",
      bottom: 0,
      zIndex: -1, 
}}>
      <SVGShape width={50} height={50} />
    </View>
  );
};

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.