I get a SVG from an API call which looks something like this:
<svg width="848" height="848" viewBox="0 0 848 848" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">xxxxxxxxx</svg>
Now I would like to display this SVG in my react component without having to create a seperate .svg file for each of my SVGs as shown in the SVGR docs like: import { ReactComponent as Ticket} from "./ticket.svg";
How can this be achieved in React vite? (with or without SVGR), the <img src={svg} />
method doesn't work for me either since it's a more complex SVG with some direct links/API calls
I am using the vite-plugin-svgr with:
vite.config
export default defineConfig({
plugins: [svgr(), react()],
});
My component
const TicketDetail = () => {
const [ticketSvg, setTicketSvg] = useState();
const getTicketSVG = () => {
// ...fetching SVG here
setTicketSvg(svg); // looks something like <svg> xxxxxxxxxxx </svg>
};
return (
<header className={styles.header}>
// display svg here
<svg>{ticketSvg}</svg>
</header>
);
};