A Guide to Effortlessly Use SVGs in Next Js Projects (2024)

In the world of web development, Scalable Vector Graphics (SVGs) have become a staple for creating scalable, high-quality graphics that look great on any screen size. However, integrating SVGs into React projects can sometimes be a hassle.

SVGR is a command-line tool and webpack loader that transforms SVG files into React components. This means that instead of dealing with cumbersome SVG syntax directly in your code, you can simply import SVG files as React components and use them just like any other React component.

Next, configure SVGR in your Next.js project. You can do this by adding SVGR to your webpack configuration.

const nextConfig = { webpack(config) { // Grab the existing rule that handles SVG imports const fileLoaderRule = config.module.rules.find(rule => rule.test?.test?.('.svg')); config.module.rules.push( // Reapply the existing rule, but only for svg imports ending in ?url { ...fileLoaderRule, test: /\.svg$/i, resourceQuery: /url/ // *.svg?url }, // Convert all other *.svg imports to React components { test: /\.svg$/i, issuer: fileLoaderRule.issuer, resourceQuery: { not: [...fileLoaderRule.resourceQuery.not, /url/] }, // exclude if *.svg?url use: ['@svgr/webpack'] } ); // Modify the file loader rule to ignore *.svg, since we have it handled now. fileLoaderRule.exclude = /\.svg$/i; return config; },};export default nextConfig;

Now that SVGR is set up, you can start using it to import SVGs as React components directly into your Next.js project. Simply import the SVG file and use it like any other React component:

import MySvgIcon from '../path/to/MySvgIcon.svg';const MyComponent = () => { return ( <div> <MySvgIcon /> {/* Other JSX */} </div> );};

SVGR provides various options for customizing the generated React components. You can configure options such as dimensions, file naming, and component naming by passing options to SVGR through webpack configuration or directly in the import statement.

For more, checkout: https://react-svgr.com/docs/getting-started/

Happy coding!

Top comments (1)

Subscribe

A Guide to Effortlessly Use SVGs in Next Js Projects (8)

Amir M. Mohamadi

I design, I code đź––

•

For those who are willing to make it work with Turbopack, check this discussion, it worked for me.

For further actions, you may consider blocking this person and/or reporting abuse

A Guide to Effortlessly Use SVGs in Next Js Projects (2024)
Top Articles
Pumpkin: Health Benefits and Nutrition Facts
Startup Failure
Chs.mywork
Compare Foods Wilson Nc
No Limit Telegram Channel
Craigslist Campers Greenville Sc
Atvs For Sale By Owner Craigslist
How to know if a financial advisor is good?
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Www Movieswood Com
Paketshops | PAKET.net
Best Pawn Shops Near Me
Simple Steamed Purple Sweet Potatoes
Lqse-2Hdc-D
Ap Chem Unit 8 Progress Check Mcq
Taylor Swift Seating Chart Nashville
Socket Exception Dunkin
Cooking Fever Wiki
A rough Sunday for some of the NFL's best teams in 2023 led to the three biggest upsets: Analysis - NFL
Q Management Inc
Lazarillo De Tormes Summary and Study Guide | SuperSummary
H12 Weidian
10 Fun Things to Do in Elk Grove, CA | Explore Elk Grove
Www.publicsurplus.com Motor Pool
Sulfur - Element information, properties and uses
Dwc Qme Database
Team C Lakewood
Reviews over Supersaver - Opiness - Spreekt uit ervaring
eugene bicycles - craigslist
Gilchrist Verband - Lumedis - Ihre Schulterspezialisten
The Banshees Of Inisherin Showtimes Near Broadway Metro
Democrat And Chronicle Obituaries For This Week
Shiny Flower Belinda
Reserve A Room Ucla
Lilpeachbutt69 Stephanie Chavez
Florence Y'alls Standings
Kamzz Llc
Ff14 Sage Stat Priority
Great Clips On Alameda
Merkantilismus – Staatslexikon
Fapello.clm
Me Tv Quizzes
The Wait Odotus 2021 Watch Online Free
QVC hosts Carolyn Gracie, Dan Hughes among 400 laid off by network's parent company
Shell Gas Stations Prices
Vérificateur De Billet Loto-Québec
Mother Cabrini, the First American Saint of the Catholic Church
Port Huron Newspaper
Backpage New York | massage in New York, New York
Race Deepwoken
60 Second Burger Run Unblocked
Rétrospective 2023 : une année culturelle de renaissances et de mutations
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 5794

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.