-1

I am getting SVG icons from backend as below response:

{
  "status": 200,
  "amenities": [
    {
      "id": 4,
      "amenities_name": "TV",
      "amenities_icon": "amenities/2AdAlkq4uNImDYGEdoUmHdzD6ldRLzSwQeSRrkKe.svg",
      "amenities_category": "accommodation",
      "created_at": "2024-03-27T13:02:25.000000Z",
      "updated_at": "2024-04-01T01:43:50.000000Z"
    },
    {
      "id": 5,
      "amenities_name": "Wifi",
      "amenities_icon": "amenities/iDNYy1PEiWtaMjfEEio0rfAqNPQKerfyetRpEEzc.svg",
      "amenities_category": "accommodation",
      "created_at": "2024-03-27T13:02:45.000000Z",
      "updated_at": "2024-03-27T13:02:45.000000Z"
    }
]
}

If I add the base URL with amenities_icon and search on the browser I am getting SVG icon with SVG tag but in React I can't do it. I am not able to convert it to an SVG tag. I load all of them inside the image tag but I need SVG tag.

I tried to convert the URL into text, and it returns a SVG tag. It works in Javascript but it is not working in React.

export const convertToSVG = async (url) => {
  try {
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const svgText = await response.text();
    return svgText;
  } catch (error) {
    console.error("Error fetching SVG:", error);
    return null; 
  }
};
New contributor
Sudipta Das is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • Why do you need an SVG tag in particular?
    – AKX
    Commented Jul 1 at 5:33
  • Could you please provide an example of how this svgText looks like? If its something like M10,30 A20,20,0,0,1,50,30 A20,20,0,0,1,90,30 Q90,60,50,90 Q10,60,10,30 Z then you could use <svg><path d={path} /></svg> Commented Jul 1 at 6:05
  • React is (for the most part) Javascript, so if your code/logic/etc works in Javascript then it should work nearly as equally well in any React code. What is the exact/specific issue or problem you have? Please edit to clarify and to provide a complete minimal reproducible example of the relevant code you are working with.
    – Drew Reese
    Commented Jul 1 at 6:50

1 Answer 1

0

you can use dangerouslySetInnerHTML attribute to set that as svg in your jsx here is the code you can use

import React, { useEffect, useState } from 'react';

const SVGIcon = ({ url }) => {
  const [svgContent, setSvgContent] = useState(null);

  const fetchSVGContent = async (url) => {
  try {
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const svgText = await response.text();
    return svgText;
  } catch (error) {
    console.error("Error fetching SVG:", error);
    return null; 
  }
};

  useEffect(() => {
    const loadSVG = async () => {
      const svg = await fetchSVGContent(url);
      setSvgContent(svg);
    };

    loadSVG();
  }, [url]);

  return (
    <div
      dangerouslySetInnerHTML={{ __html: svgContent }}
      style={{ width: '100%', height: '100%' }}
    />
  );
};

export default SVGIcon;

Not the answer you're looking for? Browse other questions tagged or ask your own question.