Skip to content

搞英语 → 看世界

翻译英文优质信息和名人推特

Menu
  • 首页
  • 作者列表
  • 独立博客
  • 专业媒体
  • 名人推特
  • 邮件列表
  • 关于本站
Menu

如何在 React 中导入 SVG

Posted on 2022-05-30

xnnaeu5l8yekw8u5r3j5.png

方法一

将其用作 img 标签的来源

import icon from ' ./icon.svg ' ; const Footer = () => { return ( < Footer > { /*... */ } < a href = ' # ' > < img src = { icon } alt = "" /> < /a > < /Footer > ) }

方法二

将其用作组件。通过这种方式,您可以使用 CSS 设置 SVG 样式。使用(storke、fill、stroke-dasharray …)等属性更改颜色或动画

import { ReactComponent as Icon } from ' ./icon.svg ' ; const Footer = () => { return ( < Footer > { /*... */ } < a href = ' # ' > < Icon /> < /a > < /Footer > ) }

方法三

直接在 JSX 中包含 SVG。这与之前的方法类似,但它可能无法针对大量 SVG 进行扩展。

 const Icon = () => { < svg xmlns = " http://www.w3.org/2000/svg " width = " 16 " height = " 16 " fill = " currentColor " viewBox = " 0 0 16 16 " > < path d = " M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z " /> < /svg > } const Footer = () => { return ( < Footer > { /*... */ } < a href = ' # ' > < Icon /> < /a > < /Footer > ) }

原文: https://dev.to/zougari47/how-to-import-svg-in-react-4ne3

本站文章系自动翻译,站长会周期检查,如果有不当内容,请点此留言,非常感谢。
  • Abhinav
  • Abigail Pain
  • Adam Fortuna
  • Alberto Gallego
  • Alex Wlchan
  • Answer.AI
  • Arne Bahlo
  • Ben Carlson
  • Ben Kuhn
  • Bert Hubert
  • Bits about Money
  • Brian Krebs
  • ByteByteGo
  • Chip Huyen
  • Chips and Cheese
  • Christopher Butler
  • Cool Infographics
  • Dan Sinker
  • David Walsh
  • Dmitry Dolzhenko
  • Elad Gil
  • Ellie Huxtable
  • Ethan Marcotte
  • Exponential View
  • FAIL Blog
  • Founder Weekly
  • Geoffrey Huntley
  • Geoffrey Litt
  • Greg Mankiw
  • Henrique Dias
  • Hypercritical
  • IEEE Spectrum
  • Investment Talk
  • Jaz
  • Jeff Geerling
  • Jonas Hietala
  • Josh Comeau
  • Lenny Rachitsky
  • Lou Plummer
  • Luke Wroblewski
  • Matt Stoller
  • Mert Bulan
  • Mostly metrics
  • News Letter
  • NextDraft
  • Non_Interactive
  • Not Boring
  • One Useful Thing
  • Phil Eaton
  • Product Market Fit
  • Readwise
  • ReedyBear
  • Robert Heaton
  • Ruben Schade
  • Sage Economics
  • Sam Altman
  • Sam Rose
  • selfh.st
  • Shtetl-Optimized
  • Simon schreibt
  • Slashdot
  • Small Good Things
  • Taylor Troesh
  • Telegram Blog
  • The Macro Compass
  • The Pomp Letter
  • thesephist
  • Thinking Deep & Wide
  • Tim Kellogg
  • 英文媒体
  • 英文推特
  • 英文独立博客
©2025 搞英语 → 看世界 | Design: Newspaperly WordPress Theme