This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

SVGR for your React app


More often than not, builders have a tendency so as to add svg pictures to an belongings listing and import them both instantly or as a React element. This course of not solely will increase your app bundle dimension but additionally makes managing all of the belongings tough.
What if there was a approach to handle all the applying icons like the best way we import them from another icon library?
Sure, react-svgr helps you handle all of the icons in your React utility.



What’s SVGR?

SVGR is an common instrument to remodel SVG into React elements.
SVGR takes a uncooked SVG and transforms it right into a ready-to-use React element.

Instance,
Uncooked svg icon,
Raw svg icon
After working SVGR, we get the under react element,
SVGR react component



The best way to use SVGR?

  1. SVGR playground
    SVGR playground will assist us rework our svg picture right into a react element on-line. Then we will copy-paste this element into our react utility for additional use.
    SVGR Playground

  2. SVGR cli
    SVGR playground is helpful provided that we’ve few icons for transformation. However with SVGR cli we will rework a single file or an entire listing.
    can set up @svgr/cli regionally in our venture:

npm set up --save-dev @svgr/cli
# or use yarn
yarn add --dev @svgr/cli
Enter fullscreen mode

Exit fullscreen mode

  • Transforms a single file by specifying file as the only argument.
npx @svgr/cli -- belongings/icons/menu-icon.svg
Enter fullscreen mode

Exit fullscreen mode

  • Remodel an entire listing
    Transforms an entire listing utilizing --out-dir choice. All SVG presents on this listing tree are remodeled into React elements.
npx @svgr/cli --out-dir dist -- icons
Enter fullscreen mode

Exit fullscreen mode



Write a script for batch transformation

With the assistance of @svgr/cli we will easy write a script which may additional merely this course of.

  1. Write new script into bundle.json,
    svgr cli script
  2. Create svgr.config.js file on root lever with under config,
    svgr config
  3. Now we’re good to execute under npm command to remodel all our svg icons into react elements.
npm run icon:create
Enter fullscreen mode

Exit fullscreen mode



And extra…

  • Test Hybrowlabs for React app improvement companies.

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

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

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?