question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to save my Svg component as a svg file ?

See original GitHub issue

hello. In my application, I made an image with react-native-svg , just like this:

<Svg height="500" width="500"  style={{backgroundColor:'#33AAFF'}}>
          <Rect
              x="50"
              y= "50"
              width="50"
              height="50"
              fill= "#3399ff"
              strokeWidth="3"
              stroke="rgb(0,0,0)"
           />
</Svg>

Now, I want to save the content as svg file,for providing to other application to use it, but I cant find any resolution to deal this.

Im a Newer in Reat-native, im work on native android before, so please give me some suggestions about this problem, that will be every thankful.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
msandcommented, Oct 19, 2019

This works well in react-native-web and web version of expo as well:

import * as React from "react";
import { Platform, StyleSheet, TouchableOpacity } from "react-native";
import { Svg, Rect } from "react-native-svg";
import ReactDOMServer from "react-dom/server";

const isWeb = Platform.OS === "web";

const childToWeb = child => {
  const { type, props } = child;
  const name = type && type.displayName;
  const webName = name && name[0].toLowerCase() + name.slice(1);
  const Tag = webName ? webName : type;
  return <Tag {...props}>{toWeb(props.children)}</Tag>;
};

const toWeb = children => React.Children.map(children, childToWeb);

export default class App extends React.Component {
  renderSvg() {
    return (
      <Svg height="100%" width="100%" style={{ backgroundColor: "#33AAFF" }}>
        <Rect
          x="50"
          y="50"
          width="50"
          height="50"
          fill="#3399ff"
          strokeWidth="3"
          stroke="rgb(0,0,0)"
        />
      </Svg>
    );
  }
  serialize = () => {
    const element = this.renderSvg();
    const webJsx = isWeb ? element : toWeb(element);
    const svgString = ReactDOMServer.renderToStaticMarkup(webJsx);
    console.log(svgString);
  };
  render() {
    return (
      <TouchableOpacity style={styles.container} onPress={this.serialize}>
        {this.renderSvg()}
      </TouchableOpacity>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    backgroundColor: "#ecf0f1",
    padding: 8
  }
});

0reactions
cjinghongcommented, Jan 30, 2021

Can this be done on mobile? I’m having issues importing “react-dom”

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to save my Svg component as a svg file ? #1142 - GitHub
Now, I want to save the content as svg file,for providing to other application to use it, but I cant find any resolution...
Read more >
React SVG: How to use SVGs best in React - CopyCat Blog
In this article, we'll look at react SVG - importing SVG into react, how to convert SVG to components, and much more.
Read more >
Export high-quality, optimized SVG - Adobe Illustrator
To export a section or component of your design to SVG, select it, and then choose File > Export Selection > SVG (svg)....
Read more >
javascript - How do I save/export an SVG file after creating an ...
get inline svg element to output. · get svg source by XMLSerializer. · add name spaces of svg and xlink. · construct url...
Read more >
Optimizing, Converting And Exporting SVG Icons In React
Optimizing SVGs · Click on Paste markup an paste the SVG code that you exported from Sketch (a.k.a. the SVG file above) ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found