This content originally appeared on DEV Community and was authored by vrauuss softwares
1. Install vite-plugin-svgr
This plugin transforms SVG files into React components. Choose your package manager to install:
npm install --save-dev vite-plugin-svgr
yarn add -D vite-plugin-svgr
pnpm add -D vite-plugin-svgr
2. Setup vite.config.ts
- First, import the plugin:
import svgr from "vite-plugin-svgr";
- Next, add the plugin to your
defineConfig
:
svgr()
- The finished structure should look like this:
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import svgr from "vite-plugin-svgr";
// https://vite.dev/config/
export default defineConfig({
plugins: [react(), svgr()]
});
3. Add Types to vite-env.d.ts
- Enable TypeScript support for SVG props like
className
,width
, andheight
.
/// <reference types="vite-plugin-svgr/client" />
4. Import with ?react
suffix and use SVG as Component
- Import the SVG file with the
?react
suffix:
import Logo from "./assets/icons/logo.svg?react";
- Use the imported SVG as a React component:
<Logo className="w-8 h-8 fill-red-500" />
Contact
- GitHub: Vrauuss Softwares
- Youtube: Vrauuss Softwares
- TikTok: Vrauuss Softwares
Articles
- Video from this article: How to import SVG as React Component with Vite and TypeScript
- Blog: Coming soon...
- Medium: Coming soon...
Upcoming Articles
- How to Change the Size of an SVG with CSS
- How to Disable SVG Preview Mode
References
This content originally appeared on DEV Community and was authored by vrauuss softwares

vrauuss softwares | Sciencx (2025-03-23T13:01:17+00:00) How to import SVG as React Component with Vite and TypeScript. Retrieved from https://www.scien.cx/2025/03/23/how-to-import-svg-as-react-component-with-vite-and-typescript/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.