<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Om Kathe</title>
    <description>The latest articles on Forem by Om Kathe (@itsomkathe).</description>
    <link>https://forem.com/itsomkathe</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F747104%2Fc3fb5549-1687-4e7d-913f-df34d82f9ebf.jpg</url>
      <title>Forem: Om Kathe</title>
      <link>https://forem.com/itsomkathe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/itsomkathe"/>
    <language>en</language>
    <item>
      <title>Use SVGs with Next.js using svgr/webpack</title>
      <dc:creator>Om Kathe</dc:creator>
      <pubDate>Fri, 04 Nov 2022 03:54:40 +0000</pubDate>
      <link>https://forem.com/itsomkathe/use-svgs-with-nextjs-using-svgrwebpack-4fb9</link>
      <guid>https://forem.com/itsomkathe/use-svgs-with-nextjs-using-svgrwebpack-4fb9</guid>
      <description>&lt;p&gt;So you want to use SVGs in Next.js as SVG and not as an image? Then this tutorial is for you!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Install &lt;a href="https://www.npmjs.com/package/@svgr/webpack" rel="noopener noreferrer"&gt;@svgr/webpack&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This library allows us to import SVG as a React component. You do NOT need to install Webpack separately.&lt;br&gt;
We'll install it as a &lt;code&gt;dev&lt;/code&gt; dependency. Run the command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @svgr/webpack --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Configure Webpack&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There will be a &lt;code&gt;next.config.js&lt;/code&gt; file in your project. If you do not have one, just create one.&lt;br&gt;
Your &lt;code&gt;next.config.js&lt;/code&gt; should look something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  swcMinify: true,
}

module.exports = nextConfig
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add the following Webpack configuration in the &lt;code&gt;nextConfig&lt;/code&gt; object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;webpack(config){
    config.module.rules.push({
      test: /\.svg$/,
      use: [{loader: '@svgr/webpack', options: {icon: true}}]
    })
    return config
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're basically pre-processing our SVG files using loaders.&lt;br&gt;
Finally, your next.config.js should look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  swcMinify: true,
  webpack(config){
    config.module.rules.push({
      test: /\.svg$/,
      use: [{loader: '@svgr/webpack', options: {icon: true}}]
    })
    return config
  }
}

module.exports = nextConfig
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Importing SVG&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Restart the &lt;code&gt;dev&lt;/code&gt; server first.&lt;br&gt;
You can now import SVG just like any other React component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import FacebookIcon from './facebook.svg'

export const Home = ()=&amp;gt;{
    return (
        &amp;lt;FacebookIcon/&amp;gt;
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And, that's it 🎉!&lt;/p&gt;

&lt;p&gt;I hope you found my article helpful. Thanks for reading my post!&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>svg</category>
      <category>react</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
