How to Load SVG with React and Webpack (2024)

Introduction

As developers, we want all our images to look sharp and nice, even when we scale them up. To achieve the level of sharpness we want for our websites using bitmap images, such as JPEGs, GIFs, and PNGs, we have to increase file size, which greatly impacts the performance of the website. Vector images, however, looks crisp regardless of screen resolution and are relatively small in size.

In this guide, we'll learn about Scalable Vector Graphics (SVG) and explore the standard way of importing SVG in a React Application bundled with Webpack.

What is SVG?

SVG is an XML-based markup language for describing two-dimensional-based vector graphics. SVG is essentially to graphics what HTML is to text.

SVG images basically take the place of traditional images in other formats like JPEG, PNG, TIFF or GIF.

Advantages of using SVG over other image formats are:

  • SVG images can be searched, indexed, scripted, and compressed
  • SVG images are scalable
  • SVG can be created and edited with any text editor
  • SVG images do NOT lose quality even if they are resized or zoomed
  • SVG images can be animated, unlike other traditional image formats

Let's take a closer look at how to use SVG images. The xml code below creates a rectangle:

rectangle.svg

 <svg version="1.1" baseProfile="full" width="300" height="200" xmlns="http://www.w3.org/2000/svg"> <rect width="100%" height="100%" fill="green" /></svg> 

The most basic way to embed an SVG via an <img> element is to reference it in the src attribute, as you'd expect. You will need a height or a width attribute (or both if your SVG has no inherent aspect ratio).

 <img src="rectangle.svg" alt="A Rectangle Image with SVG" height="45px" width="45px" /> 

The easiest way of implementing an SVG in a React app is as follows:

 const App = () => <img src="/images/rectangle.svg" alt="A Rectangle Image with SVG" />; 

However, in most cases, SVGs are usually pretty small, and in these cases we are better off inlining the images as data URLs, limiting the amount of requests a browser has to make to the server in order to render the webpage.

To transform an SVG image into a Data URL, we will need an appropriate webpack loader in our bundler. The most common webpack loader for this is svg-url-loader, which can be added as shown below:

 npm i svg-url-loader --save-dev 

Add to webpack.config.js:

 const webpack = require('webpack');module.exports = { entry: './src/index.js', module: { rules: [ { test: /\.(js|jsx)$/, exclude: /node_modules/, use: ['babel-loader'], }, { test: /\.svg$/, use: [ { loader: 'svg-url-loader', options: { limit: 10000, }, }, ], }, ], }, ...}; 

Import the file into your React app:

 import rectangle from 'images/rectangle.svg';const App = () => <img src={rectangle} alt="" />; 

The output in the DOM will be something like this:

 <img src="ebbc8779488b4073081931bd519478e8.svg" alt="" /> 

You can check out svg-url-loader for more on the configurations.

In the examples above, the svg-url-loader can only be used in the traditional way, including images in a web application such as background-image or content. The next question is how to use an SVG image as a React Component.

SVG as a React Component

The solution is not farfetched. We'll need the help of another awesome webpack loader called svgr, which, according to the website, transforms the SVG into a ready-to-use React component.

So how do we use this awesome tool? Lets start by updating our webpack.config.js:

 const webpack = require('webpack');module.exports = { entry: './src/index.js', module: { rules: [ { test: /\.(js|jsx)$/, exclude: /node_modules/, use: ['babel-loader'], }, { test: /\.svg$/, use: ['@svgr/webpack'], }, ], }, ...}; 

Then we can install it as a dev-dependency in the usual way:

 npm install @svgr/webpack --save-dev 

Once you start your application, Webpack will do its thing and you don't need to worry about your SVGs anymore. You can put your SVG files anywhere in your src/ folder and import them wherever you need them as React components.

Lastly, import the file in your React app:

 import Image from 'path/image.svg';const App = () => ( <div> <Image /> </div>) 

This method is generally referred to as inline-svg.

A lightweight alternative solution to svgr is react-svg-loader.

 const webpack = require('webpack');module.exports = { entry: './src/index.js', module: { rules: [ { test: /\.(js|jsx)$/, exclude: /node_modules/, use: ['babel-loader'], }, { loader: 'react-svg-loader', options: { jsx: true // true outputs JSX tags } } ], }, ...}; 

And install as usual:

 npm install react-svg-loader --save-dev 

For the most part, we do not want all our SVG files to be loaded as a React components. We could combine the above methods depending on the use case. All we have to do is update our webpack configuration.

The example below uses svgr and url-loader, a webpack loader which transforms files into base64 URIs.

 const webpack = require('webpack');module.exports = { entry: './src/index.js', module: { rules: [ { test: /\.(js|jsx)$/, exclude: /node_modules/, use: ['babel-loader'], }, { test: /\.svg$/, use: ['@svgr/webpack', 'url-loader'], }, ], }, ...}; 

The usage in your application would look this:

 import imageUrl { ReactComponent as Image } from 'path/image.svg';const App = () => ( <div> <img src={imageUrl} alt="" /> <Image /> </div>) 

Conclusion

This guide has provided you with a quick intro into how to import SVG into your React Application using webpack as the bundler.

Here are a few other resources on SVG and its numerous applications:

How to Load SVG with React and Webpack (2024)
Top Articles
Billing Statement: Definition, Key Details, How to Read
Which side of Costa Rica has the best beaches for families?
Party City.near.me
Google Alerts Login
Chokenigg*s
When His Eyes Opened Chapter 191
We at yo momma house train Full video
How To Add An ESIM With A QR Code - Easy Guide | ESIM2Fly.com
Irrationale Kitchen
Ticket To Paradise Showtimes Near Apple Cinemas Waterbury
Funeral Homes in Grand Forks, North Dakota
Spring 2023 Etam Results
Https://Cbdwarehousestore.com/Product-Category/Liquid-Herbal-Incense/
Pokémon Infinite Fusion Calculator- Generate Custom Pokémon Fusions
Affidea Skarżysko-Kamienna, Skarżysko-Kamienna Reviews | Medical diagnostic imaging center
Fnv Turbo
Yuliett Torres Lives
Cryoaudiovascularmalexia
80 For Brady Showtimes Near Cinemark At Harlingen
Anderson Preparatory Academy Skyward
O'reilly's Duquoin Illinois
Nipr To Sipr File Transfer Dots
Deshuesadero El Pulpo
Yxssied
This Modern World Daily Kos
What Is 8/12 As A Grade
Days Calculator 📅 (Days Between Dates)
Ixl Ld Northeast
Hardest Game Unblocked Games
Hot Fuzz Putlocker
Sparta Ymca
Man City hearing: Premier League 115 charges case begins
Blak Stellenanzeigen
Csl Plasma Wilkes-Barre Township Reviews
Security Awareness Training
Joy Ride movie review & film summary (2023) | Roger Ebert
Hope anchors the soul Zipper Pouch | CafePress
Www.craigslist.com Savannah Ga
Comcast Business Downdetector
What Happened to Brookelyn Farthing? Is She Dead or Alive?
Beth Israel Farr Building Parking
Www Craigslist Denver Com
Vinyl record sales continue resurgence
Gatlinburg Trolley Schedule 2022
Inland Empire Craigslist Macy's
Saint Frances Cabrini West Bend Wi
Holly Holm Camel Toe
Joy Ride 2023 Showtimes Near Amc Ward Parkway
Fatal Accident In Nashville Tn Today
2011 Hyundai Sonata 2 4 Serpentine Belt Diagram
Is Ryancomedy Gay
Does Gamestop Take Airpods
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated:

Views: 6250

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.