<?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>DEV Community: Sandro Miguel Marques</title>
    <description>The latest articles on DEV Community by Sandro Miguel Marques (@sandromiguel).</description>
    <link>https://dev.to/sandromiguel</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%2F321051%2F95c29666-18c5-47d3-957a-145c0cec311f.jpg</url>
      <title>DEV Community: Sandro Miguel Marques</title>
      <link>https://dev.to/sandromiguel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sandromiguel"/>
    <language>en</language>
    <item>
      <title>Customize SVGs in React</title>
      <dc:creator>Sandro Miguel Marques</dc:creator>
      <pubDate>Sat, 19 Dec 2020 11:26:11 +0000</pubDate>
      <link>https://dev.to/sandromiguel/customize-svgs-in-react-4g90</link>
      <guid>https://dev.to/sandromiguel/customize-svgs-in-react-4g90</guid>
      <description>&lt;p&gt;I tried a few ways to display SVG images in React and I want to share my choice.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Packages with SVG icon collections&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;img&lt;/code&gt; tag: &lt;code&gt;&amp;lt;img src="someSvg" alt="some text" /&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  Copy/paste &lt;code&gt;svg&lt;/code&gt; tag into JSX: &lt;code&gt;&amp;lt;svg&amp;gt;...&amp;lt;/svg&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  Import SVG as React component &lt;strong&gt;&amp;lt;– my choice&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Advantages of using SVGs as a React component
&lt;/h2&gt;

&lt;p&gt;Why did I choose to use SVGs as a React component?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  No need to install icon collections;&lt;/li&gt;
&lt;li&gt;  I can do customization through props: &lt;code&gt;width&lt;/code&gt;, &lt;code&gt;height&lt;/code&gt;, &lt;code&gt;fill&lt;/code&gt;, &lt;code&gt;stroke&lt;/code&gt;, &lt;code&gt;strokeWith&lt;/code&gt;, etc.;&lt;/li&gt;
&lt;li&gt;  I can define other styles with CSS through &lt;code&gt;className&lt;/code&gt; prop;&lt;/li&gt;
&lt;li&gt;  No need to copy/paste the SVG file;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Usage with Create React App
&lt;/h2&gt;

&lt;p&gt;If you use CRA you can import SVG files and use them as React component right away.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;App.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;- import logo from "./logo.svg";
&lt;/span&gt;&lt;span class="gi"&gt;+ import { ReactComponent as Logo } from "./logo.svg";
&lt;/span&gt;&lt;span class="p"&gt;import "./App.css";
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;function App() {
&lt;/span&gt;  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;header className="App-header"&amp;gt;
&lt;span class="gd"&gt;-       &amp;lt;img src={logo} alt="logo" /&amp;gt;
&lt;/span&gt;&lt;span class="gi"&gt;+       &amp;lt;Logo stroke="#f60" strokeWidth={10} width={500} /&amp;gt;
&lt;/span&gt;        &amp;lt;p&amp;gt;
          Edit &amp;lt;code&amp;gt;src/App.js&amp;lt;/code&amp;gt; and save to reload.
        &amp;lt;/p&amp;gt;
        &amp;lt;a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        &amp;gt;
          Learn React
        &amp;lt;/a&amp;gt;
      &amp;lt;/header&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;export default App;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7xbj5lfo8seght92j44n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7xbj5lfo8seght92j44n.png" alt="Alt Text" width="765" height="631"&gt;&lt;/a&gt;&lt;br&gt;
Stroke changed logo&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage without Create React App
&lt;/h2&gt;

&lt;p&gt;Create React App uses &lt;a href="https://react-svgr.com/" rel="noopener noreferrer"&gt;SVGR&lt;/a&gt; behind the scenes to import SVG files as a React component, so let’s do the same now.&lt;/p&gt;

&lt;h3&gt;
  
  
  1 - Install the Webpack loader called &lt;code&gt;@svgr/webpack&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add --dev @svgr/webpack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2 - Update your &lt;code&gt;webpack.config.js&lt;/code&gt;
&lt;/h3&gt;



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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: I use it in conjunction with &lt;code&gt;&lt;a href="https://github.com/webpack-contrib/file-loader" rel="noopener noreferrer"&gt;file-loader&lt;/a&gt;&lt;/code&gt;, in case I want to import SVG files normally.&lt;/p&gt;

&lt;h3&gt;
  
  
  3 - Import SVG files as React component
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ReactComponent as SomeImage } from 'path/to/image.svg'

...

&amp;lt;SomeImage width={100} height={50} fill="pink" stroke="#0066ff" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  Transform SVGs into React components — &lt;a href="https://react-svgr.com/" rel="noopener noreferrer"&gt;SVGR&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  File Loader — &lt;code&gt;&lt;a href="https://github.com/webpack-contrib/file-loader" rel="noopener noreferrer"&gt;file-loader&lt;/a&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  Cover photo by &lt;a href="https://unsplash.com/@aquatium?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Harpal Singh&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/icons?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Summing it up, using SVGs as a React component allows me to easily customize my SVG icons in a snap. That’s what I want.&lt;/p&gt;

</description>
      <category>react</category>
      <category>svg</category>
      <category>svgr</category>
    </item>
  </channel>
</rss>
