Unable to import SVG with Vite as ReactComponent

👉 Use vite-plugin-svgr

  1. Add SVGR to the project

yarn add -D @honkhonk/vite-plugin-svgr

  1. Add the plugin SVGR to vite in vite.config.js
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import svgr from '@honkhonk/vite-plugin-svgr'

export default defineConfig({
  plugins: [ svgr(), react()]
})
  1. In App.tsx import SVG as React Component by adding ?component on the svg's import directive :
import Happy from './assets/svg/happy.svg?component'

<Happy />