SVG Gebruiken in React en Next.js: Complete Integratiegids
Beheers SVG-integratie in React- en Next.js-applicaties. Leer importmethoden, componentpatronen, optimalisatie en best practices voor moderne webontwikkeling.

Founder of VectoSolve
Self-taught developer, founder and sole developer of VectoSolve since 2024. He builds the conversion engine and the cutting, embroidery and 3D printing exports.
How this article was made: drafted with AI assistance, then edited and fact-checked before publication. About the author
Key Takeaways
- React biedt vier belangrijke SVG-importmethoden: afbeelding-import, inline SVG, SVGR-componenten en dynamische imports
- SVGR met Next.js webpack-configuratie geeft de beste balans tussen ontwikkelaarservaring en runtime-controle
- Herbruikbare icon-componenten met TypeScript-props (size, color, className) houden je code DRY
- Framer Motion integreert native met SVG-elementen voor spring-gebaseerde pad- en vormanimaties
-
Voeg altijd
aria-labelofaria-hiddentoe aan SVG-iconen voor screenreader-toegankelijkheid
SVG in Moderne React-Ontwikkeling
SVGs zijn essentieel voor moderne React-applicaties en bieden scherpe graphics op elke schaal. Deze gids behandelt elke methode van SVG-gebruik in React- en Next.js-projecten.
Importmethoden
1. Standaard Afbeelding-Import
Eenvoudigste aanpak:
import logo from './logo.svg';
function Header() {
return <img src={logo} alt="Logo" className="logo" />;
}
Voordelen: Eenvoudig, vertrouwd
Nadelen: Geen styling-/manipulatietoegang
2. Inline SVG-Componenten
Volle controle:
function Icon() {
return (
<svg viewBox="0 0 24 24" className="icon">
<path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5"/>
</svg>
);
}
Voordelen: Volledige CSS/JS-toegang
Nadelen: Uitgebreid, bron moeilijk te wijzigen
3. SVGR (React-Component)
Importeren als component:
import { ReactComponent as Logo } from './logo.svg';
function Header() {
return <Logo className="logo" fill="currentColor" />;
}
Voordelen: Het beste van beide werelden
Nadelen: Vereist configuratie
4. Dynamische Import
Voor code splitting:
import dynamic from 'next/dynamic';
const DynamicIcon = dynamic(() => import('./icons/chart.svg'));
function Dashboard() {
return <DynamicIcon />;
}
Pro Tip: Voor de meeste React-projecten is SVGR de aanbevolen aanpak. Het laat je SVGs importeren als React-componenten met volledige prop-ondersteuning, terwijl SVG-bestanden apart blijven voor eenvoudige bewerking in design-tools.
SVGR-Configuratie
Next.js Setup
// next.config.js
module.exports = {
webpack(config) {
config.module.rules.push({
test: /\.svg$/,
use: ['@svgr/webpack'],
});
return config;
},
};
Met URL-Fallback
// next.config.js
module.exports = {
webpack(config) {
config.module.rules.push({
test: /\.svg$/,
issuer: /\.[jt]sx?$/,
use: [
{
loader: '@svgr/webpack',
options: {
svgoConfig: {
plugins: [{
name: 'removeViewBox',
active: false
}]
}
}
},
'url-loader'
],
});
return config;
},
};
A Full Year of Conversions for $49
Starter Annual: 25 conversions every month, $49 for the whole year. That is about $4.08 a month · Cancel anytime
Get the Annual DealComponentpatronen
Herbruikbare Icon-Component
interface IconProps {
name: string;
size?: number;
color?: string;
className?: string;
}
export function Icon({ name, size = 24, color = 'currentColor', className }: IconProps) {
return (
<svg
width={size}
height={size}
fill={color}
className={className}
>
<use href={`/sprites.svg#${name}`} />
</svg>
);
}
// Gebruik
<Icon name="search" size={20} color="#333" />
Icon-Bibliotheek Component
// icons/index.tsx
import { FC, SVGProps } from 'react';
interface IconComponentProps extends SVGProps<SVGSVGElement> {
size?: number;
}
const createIcon = (path: string): FC<IconComponentProps> => {
return function IconComponent({ size = 24, ...props }) {
return (
<svg
viewBox="0 0 24 24"
width={size}
height={size}
fill="currentColor"
{...props}
>
<path d={path} />
</svg>
);
};
};
export const SearchIcon = createIcon('M15.5 14h-.79l-.28-.27...');
export const HomeIcon = createIcon('M10 20v-6h4v6h5v-8h3L12...');
SVG Stylen in React
CSS-Styling
/* styles.css */
.icon {
width: 24px;
height: 24px;
fill: currentColor;
transition: fill 0.2s;
}
.icon:hover {
fill: #0066cc;
}
Styled Components
import styled from 'styled-components';
const StyledSvg = styled.svg`
width: ${props => props.size || 24}px;
height: ${props => props.size || 24}px;
fill: ${props => props.color || 'currentColor'};
transition: fill 0.2s;
&:hover {
fill: ${props => props.hoverColor || props.color};
}
`;
Tailwind CSS
<svg
className="w-6 h-6 fill-current text-gray-600 hover:text-blue-500 transition-colors"
viewBox="0 0 24 24"
>
<path d="..."/>
</svg>
"De beste SVG-workflow in React is die waarbij designers schone vectoren exporteren en ontwikkelaars ze importeren als eersteklas componenten — geen handmatige conversiestappen ertussen.
Animatie in React
CSS-Animatie
function LoadingSpinner() {
return (
<svg className="animate-spin" viewBox="0 0 24 24">
<circle
className="opacity-25"
cx="12" cy="12" r="10"
stroke="currentColor" strokeWidth="4"
/>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"
/>
</svg>
);
}
Framer Motion
import { motion } from 'framer-motion';
function AnimatedIcon() {
return (
<motion.svg
viewBox="0 0 24 24"
initial={{ scale: 0 }}
animate={{ scale: 1 }}
transition={{ type: 'spring' }}
>
<motion.path
d="M12 2L2 22h20L12 2z"
initial={{ pathLength: 0 }}
animate={{ pathLength: 1 }}
/>
</motion.svg>
);
}
Free SVG Tools for Crafters
Design Checker · Color Separator · Mat Preview · Offset Generator · Text to SVG
Check Your SVG FreeOptimalisatiestrategieën
Build-Time Optimalisatie
// svgo.config.js
module.exports = {
plugins: [
'preset-default',
'removeDimensions',
{ name: 'removeViewBox', active: false },
{ name: 'prefixIds', params: { prefix: 'svg-' } }
]
};
Code Splitting
// Zware iconen lazy laden
const ChartIcon = lazy(() => import('./icons/Chart'));
function Dashboard() {
return (
<Suspense fallback={<div className="icon-placeholder" />}>
<ChartIcon />
</Suspense>
);
}
Verwijder nooit viewBox tijdens SVGO-optimalisatie. Zonder viewBox verliezen SVGs hun vermogen om responsief te schalen. Stel altijd removeViewBox: false in je SVGO-configuratie in.
Toegankelijkheid in React
Toegankelijk Icon-Patroon
interface AccessibleIconProps {
label: string;
children: React.ReactNode;
}
function AccessibleIcon({ label, children }: AccessibleIconProps) {
return (
<span role="img" aria-label={label}>
{children}
</span>
);
}
// Gebruik
<AccessibleIcon label="Zoeken">
<SearchIcon />
</AccessibleIcon>
Icon-Knop
function IconButton({ icon: Icon, label, onClick }) {
return (
<button
onClick={onClick}
aria-label={label}
className="icon-button"
>
<Icon aria-hidden="true" />
</button>
);
}
VectoSolve-Integratie
Workflow
VectoSolve gebruiken met React:
- Afbeeldingen converteren naar SVG
- Geoptimaliseerde uitvoer downloaden
- Importeren in je React-project
- Bovenstaande voorkeursmethode gebruiken
Voordelen van Geoptimaliseerde Bestanden
VectoSolve SVGs zijn:
- Geoptimaliseerd voor webgebruik
- Klaar voor SVGR-import
- Schone paddata
- Eenvoudig te stylen
A Full Year of Conversions for $49
Starter Annual: 25 conversions every month, $49 for the whole year. That is about $4.08 a month · Cancel anytime
Get the Annual DealConclusie
React biedt meerdere manieren om met SVGs te werken, elk met afwegingen. Kies de aanpak die bij je behoeften past: eenvoudige afbeelding-imports voor basisgebruik, SVGR voor volledige controle, of componentpatronen voor icoon-systemen. Begin met schone, geoptimaliseerde SVGs van VectoSolve voor de beste resultaten.
| SVG-Importmethode | Runtime-Controle | Bundle-Impact | Ideaal Voor |
|---|---|---|---|
| <img src> import | Geen | Aparte HTTP-request | Statische decoratieve afbeeldingen |
| Inline JSX SVG | Volledig (props, events, CSS) | Verhoogt componentgrootte | Interactieve iconen, animaties |
| SVGR-component | Volledig (getypeerde props, tree-shaking) | Chunk per component | Icoon-bibliotheken, design systems |
| Dynamische import | Volledig (na laden) | Code-split, lazy loaded | Below-the-fold of conditionele SVGs |
Bronnen & Verder Lezen
- MDN Web Docs: SVG in HTML — Referentie voor het inbedden van SVG in JSX en het begrijpen van SVG-DOM-interactie
- web.dev: Code Splitting — Google's gids voor lazy loading en dynamische imports voor optimale bundlegrootte
- CSS-Tricks: SVG in React — Praktische patronen voor het gebruik van SVG-componenten in React- en Next.js-projecten
- Smashing Magazine: React Components — Best practices voor het bouwen van herbruikbare, toegankelijke SVG-componentbibliotheken