Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
How to render svg file in react?

I want to upload a svg file and show preview in react, but I don't know how to convert a svg file into react component. I try to use DOMParser to parse svg-xml string to Dodument Object, but it is not working.

import { useState } from 'react';
import { Upload, Button, Card } from 'antd';
import { UploadChangeParam } from 'antd/lib/upload/interface';
import 'antd/dist/antd.css';

function App() {
  const [svgs, setSvgs] = useState<Document[]>([]);

  const handleChange = async ({ file }: UploadChangeParam) => {
    console.log(file);
    let domparser = new DOMParser();
    const svg = (await file.originFileObj?.text()) ?? '';
    const ele = domparser.parseFromString(svg, 'image/svg+xml');
    setSvgs([...svgs, ele]);
  };

  return (
    <div style={{ margin: 50 }}>
      <Upload name="file" showUploadList={false} onChange={handleChange}>
        <Button>Upload</Button>
      </Upload>
      {/* preview list */}
      <Card style={{ marginTop: 20 }}>
        {svgs.map((SVGComponent) => {
          return <SVGComponent />;
        })}
      </Card>
    </div>
  );
}

export default App;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use following library to convert svg's. You can also add its cli commands to your package.json

https://react-svgr.com/playground/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!