TASIOMIND.DEV — OPERATIONAL▸▸▸FULL STACK DEVELOPER @ GWQ SERVICEPLUS AG▸▸▸FOUNDER — K8SGPT.AI▸▸▸OPEN SOURCE: ACTIVE▸▸▸DISTRIBUTED SYSTEMS / KUBERNETES / AI▸▸▸RUST + GO + PYTHON▸▸▸FIELD TESTED / STATUS — NOMINAL▸▸▸LOCATION: EUROPE/BERLIN▸▸▸TASIOMIND.DEV — OPERATIONAL▸▸▸FULL STACK DEVELOPER @ GWQ SERVICEPLUS AG▸▸▸FOUNDER — K8SGPT.AI▸▸▸OPEN SOURCE: ACTIVE▸▸▸DISTRIBUTED SYSTEMS / KUBERNETES / AI▸▸▸RUST + GO + PYTHON▸▸▸FIELD TESTED / STATUS — NOMINAL▸▸▸LOCATION: EUROPE/BERLIN▸▸▸

Responsive SVGs in React

July 15, 2019

tl;dr: You need to import the responsive svg as a ReactComponent

Firstly, you need a responsive SVG. And then you need to inport it as a ReactComponent in React

code
import React from "react";

import logo from "/images/notes/reactjs/logo-responsive.svg";
import { ReactComponent as Logo } from "/images/notes/reactjs/logo-responsive.svg";

function Demo() {
  return (
    <div className="App-body">
      <main>
        <h1>Responsive SVG Demo</h1>

        {/* the img will show but will not be responsive */}
        <img src={logo} alt="logo" />

        {/* the ReactComponent will be a respnsive SVG, but it'll add the entire SVG, 
         i.e. ALL the <symbol>s you defined will be in the code */}
        <Logo />
      </main>
    </div>
  );
}

export default Demo;