Summary of various methods of using svg in react (with code)
This article introduces to you a summary of various methods of using svg in react (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Advantage
SVG can be read and modified by many tools (such as vscode)
No distortion, the image is clear when zoomed in or out
SVG files are pure XML and also a DOM structure
Easy to use, design software can directly export
Compatibility
Previous compatibility chart, or go to caniuse.com to view
How to use
Use directly in the template
const Home = () => ( <div> <svg> <rect></rect> </svg> </div> )
- If each svg needs to be drawn by the front end, the requirements for the front end are higher.
- If each icon is copied from the svg code given by the designer, various labels must be changed, and an error will be reported if the error is corrected
Convert svg to font
iconfont
Convert svg into react component
Conversion before project construction
Example:typescript-react-svg-icon-generator, we need a pre-command to convert svg.// svg-generator.js const generator = require('typescript-react-svg-icon-generator') const config = { svgDir: './svg/', destination: './Icon/index.tsx' } generator(config)
$ node ./svg-generator.js
import Icon from './Icon' export default class App extends Component { render() { return <icon></icon> } }
Conversion when building the project
Example:@svgr/webpackYes. Yes, this is a webpack loader.// webpack rules config { test: /\.svg$/, loader: '@svgr/webpack' }
// 全局声明svg component定义 declare interface SvgrComponent extends React.StatelessComponent<react.svgattributes>> {} declare module '*.svg' { const content: SvgrComponent export default content }</react.svgattributes>
import IconReact from '@assets/svg/react.svg' const Home = () => ( <p> <iconreact></iconreact> </p> )
The role of svg path: How to use svg path in web development
How Storage Event implements inter-page communication
The above is the detailed content of Summary of various methods of using svg in react (with code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Calling method: 1. Calls in class components can be implemented by using React.createRef(), functional declaration of ref or props custom onRef attribute; 2. Calls in function components and Hook components can be implemented by using useImperativeHandle or forwardRef to throw a child Component ref is implemented.

JavaScript does not provide any memory management operations. Instead, memory is managed by the JavaScript VM through a memory reclamation process called garbage collection.

How to use SVG to achieve image mosaic effect without using Javascript? The following article will give you a detailed understanding, I hope it will be helpful to you!

Question: How to use require to dynamically introduce static resources such as images in a Vue3+TypeScript+Vite project! Description: When developing a project today (the project framework is Vue3+TypeScript+Vite), it is necessary to dynamically introduce static resources, that is, the src attribute value of the img tag is dynamically obtained. According to the past practice, it can be directly introduced by require. The following code: Write After uploading the code, a wavy line error is reported, and the error message is: the name "require" cannot be found. Need to install type definitions for node? Try npmi --save-dev@types/node. ts(2580) after running npmi--save-d

Overview of how to use Redis and TypeScript to develop high-performance computing functions: Redis is an open source in-memory data structure storage system with high performance and scalability. TypeScript is a superset of JavaScript that provides a type system and better development tool support. Combining Redis and TypeScript, we can develop efficient computing functions to process large data sets and make full use of Redis's memory storage and computing capabilities. This article will show you how to

How to declare a type with field name enum? By design, the type field should be an enumeration value and should not be set arbitrarily by the caller. The following is the enumeration declaration of Type, with a total of 6 fields. enumType{primary="primary",success="success",warning="warning",warn="warn",//warningaliasdanger="danger",info="info",}TypeSc

svg can be converted to jpg format by using image processing software, using online conversion tools, and using the Python image processing library. Detailed introduction: 1. Image processing software includes Adobe Illustrator, Inkscape and GIMP; 2. Online conversion tools include CloudConvert, Zamzar, Online Convert, etc.; 3. Python image processing library, etc.

How to implement data type conversion function in TypeScript using MySQL Introduction: Data type conversion is a very common requirement when developing web applications. When processing data stored in a database, especially when using MySQL as the back-end database, we often need to convert the data in the query results to the type we require. This article will introduce how to use MySQL to implement data type conversion in TypeScript and provide code examples. 1. Preparation: Starting
