Transform an SVG into a React Component with SVGR

栏目: IT技术 · 发布时间: 3年前

内容简介:SVGs (Scalable Vector Graphics) are an XML-based vector images used to define 2D graphics. An SVG file has theSVGs are used in websites/webpages to seamlessly integrate 2D graphics into HTML. Recently, with the rise of Web Frameworks (Svelte, React, Angula

How to transform SVGs into a collection of reusable React components.

Transform an SVG into a React Component with SVGR

SVGs (Scalable Vector Graphics) are an XML-based vector images used to define 2D graphics. An SVG file has the .svg file extension.

SVGs are used in websites/webpages to seamlessly integrate 2D graphics into HTML. Recently, with the rise of Web Frameworks (Svelte, React, Angular, React, etc) SVGs have been a major occurrence and now largely used to embed images in components as icons.

In this post, we‘ll convert SVGs to React components using SVGR .

We will then publish them, as independent components, to Bit.dev , a cloud component hub, and make them available for all our projects.

Why convert SVGs into React components?

  • Size reduction: SVG-to-React component conversion shaves off some excess SVG styling.
  • No extra requests are made by the browser to fetch SVG images since they are now bundled with the JS.
  • It allows for easier control and maintenance of SVGs since they are now React components.
  • Verbose and obscure SVGs can be converted to customizable React components that expose a simple declarative API.

SVG and React

A common example of an SVG being used in React is non-other than the famous logo.svg generated by create-react-app when scaffolding a new React project.

- react-prj/
    -src/
        - logo.svg
        ...
    ...
    - package.json

The logo.svg is the React logo:

Converting an SVG to a React component simply means creating a React component that renders an SVG. This does away with having extra .svg files lying around.

For example, we can create a React component that returns the React logo.svg XML:

Then, we can use it anywhere:

There is no <img src={logo} className="App-logo" alt="logo" /> . It's been replaced with <ReactLogo /> . And there will be no need of logo.svg because everything is in the ReactLogo component.

Here we manually converted the React icon SVG into a React component, but we can save ourselves some time by using SVGR to automate this process.

Using SVGR in the Command-Line

Transform an SVG into a React Component with SVGR

To use SVGR from command-line, install the CLI tool:

npm install @svgr/cli

Then, we call SVGR in the command-line, passing in the .svg file and other flags to replace different attributes with our own values.

svgr --icon --replace-attr-values "#063855=currentColor" logo.svg

The above command will take our logo.svg file and convert it to a React component.

We can then copy the output shown in our terminal and create a new Logo.jsx component.

Publish Converted Components to Bit.dev

Transform an SVG into a React Component with SVGR
Bit.dev

Now that we have our logo.svg file as a React component Logo.jsx we can publish it to our collection on Bit. This way, we’ll make it available for our team to use in other projects.

Let’s install Bit globally on our machine:

$ npm install bit-bin --global

We’ll now initialize a Bit workspace in our project’s root directory:

$ cd svg-icons-project
$ bit init

We can add multiple components at once but for this small demo we’ll specifically pick our Logo component:

$ bit add src/Logo.jsx

Now, since it is a React component we’ll need to configure a compiler for it (otherwise it would be coupled to our projects build setup — we don’t want that. We want our published components to move freely from one project to another).

The compiler is another component published on Bit. The --compiler flag is our way to mention it should be used as a compiler.

$ bit import bit.envs/compilers/react --compiler

We’ll now tag our component with a version number

$ bit add --all 1.0.0

Login to our account (via our terminal)

$ bit login

And, finally, publish (‘export’) our component to the collection we’ve created on Bit.dev .

$ bit export chidume.svg-components

I’ve published another two converted components. Feel free to visit my component collection .

Transform an SVG into a React Component with SVGR
My SVGs converted to React components and published to Bit.dev

以上所述就是小编给大家介绍的《Transform an SVG into a React Component with SVGR》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

IT大败局

IT大败局

Merrill R.Chapman、周良忠 / 周良忠 / 电子工业出版社 / 2004-8-1 / 35.00

这是一本由作者亲身经历写就的MBA式教案。通过作者那专业人士的敏锐、活泼流畅的文笔和美国人特有的幽默,本书为我们剖析了IT界十个有代表性且影响深远的愚蠢败局。这十个败局涉及企业经营的十个主要方面,它们是:产业标准的魔力,“缩水”产品的阴霾,产品定位的泥潭,市场关系的教训,巨型企业的困惑,企业并购的陷阱,品牌战略的迷茫,技术导向的失衡,企业公关的真谛和科技虚幻的诱惑。 书中有许多鲜为人......一起来看看 《IT大败局》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具