Importing SVG files as React components with Vite

Read Time:57 Second

Chances are if you’ve ever used create-react-app and you wanted to import an SVG file as a React component, you did something like this and it Just Worked™:

Enter fullscreen mode Exit fullscreen mode

But, alas, if you have moved to using Vite more often, as I have, you can’t just do that.

Never fear, it’s still possible!

Enter vite-plugin-svgr

vite-plugin-svgr is a Vite plugin to transform SVGs into React components! It’s super intuitive to use:

Enter fullscreen mode Exit fullscreen mode

Add it to your vite.config.js:

Enter fullscreen mode Exit fullscreen mode

And boom, you get your expected behavior!

Enter fullscreen mode Exit fullscreen mode

This is particularly useful if you’re using a library like MUI and you need to use a custom icon, like so:

Enter fullscreen mode Exit fullscreen mode

There’s other things you can do with vite-plugin-svgr, and there’s a list of options here.

Hope that’s helpful for ya, happy icon-ing!

Source: https://dev.to/cassidoo/importing-svg-files-as-react-components-with-vite-l3n

WP Ad Inserter plugin for WordPress

Tag Cloud

Java Java Logical Programs OTP Generation in Java python Recursion youtube video ASCII Upper and Lower Case blockchain javascript graph learn to code software development Successful Software Engineers breadth first search Java Array Programs Java Programs Uncategorized android ios programming kotlin web-development django data sql cybersecurity database swiftui serverless aws swift rust react background-position gradients loader mask grid nth-child pseudo elements indieweb WordPress Print Array without brackets C++ factorial Java String Programs Final Keyword Static Variable Axie Infinity Cryptokitties NFT games tool inserting MISC Tips Codes python code python projects python3 system info python project Bigginers How to Do Integrations Payment Gateways PHP checkout page in php Implement stripe payment gateway in Step by step in PHP integrate stripe gatway in php mysql payment gateway integration in php step by step payment gateway integration in php step by step with source code payment gateway integration in website PHP Integrate Stripe Payment Gateway Tutorial PHP shopping cart checkout code shopping cart in php stripe php checkout PHP/MySQL/JSON best international payment gateway does google pay accept international payments how to accept international payments in india paytm payment gateway razorpay codeigniter github razorpay custom checkout github razorpay get payment details razorpay integration in codeigniter github razorpay international payments Razorpay payment gateway integration in CodeIgniter razorpay payment gateway integration in php code Razorpay payment gateway integration with PHP and CodeIgniter Razorpay payment gateway setup in CodeIgniter Library & Frameworks Tips & Tricks UI/UX & Front-end coding birds online html code for google sign in login with google account in PHP login with google account using javascript login with google account using javascript codeigniter login with google account using php login with google account using php source code
Using Notion as a Headless CMS with Nuxt Previous post Using Notion as a Headless CMS with Nuxt
Revolutionizing the Web with WebAssembly: A Comprehensive Guide Next post Revolutionizing the Web with WebAssembly: A Comprehensive Guide

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.