React-SVG 项目使用教程

React-SVG 项目使用教程

react-svg:art: A React component that injects SVG into the DOM.项目地址:https://gitcode.com/gh_mirrors/re/react-svg

1. 项目的目录结构及介绍

React-SVG 项目的目录结构如下:

react-svg/
├── examples/
│   ├── basic/
│   ├── dynamic-props/
│   ├── fallback/
│   ├── inject-style/
│   ├── loader/
│   ├── proxy/
│   ├── redraw-on-resize/
│   ├── render-props/
│   ├── root-props/
│   ├── title-props/
│   └── wrapper-props/
├── src/
│   ├── index.tsx
│   ├── react-svg.tsx
│   ├── types.ts
│   └── utils/
├── .babelrc
├── .editorconfig
├── .eslintrc
├── .gitignore
├── .npmignore
├── .prettierrc
├── CHANGELOG.md
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── package.json
├── README.md
├── tsconfig.json
└── webpack.config.js

目录结构介绍

  • examples/: 包含多个示例项目,展示了 React-SVG 的不同使用场景。
  • src/: 项目的源代码目录,包含主要的组件和工具函数。
    • index.tsx: 项目的入口文件。
    • react-svg.tsx: 主要的 React-SVG 组件。
    • types.ts: 类型定义文件。
    • utils/: 工具函数目录。
  • .babelrc, .editorconfig, .eslintrc, .gitignore, .npmignore, .prettierrc: 项目配置文件。
  • CHANGELOG.md, CODE_OF_CONDUCT.md, CONTRIBUTING.md, LICENSE, README.md: 项目文档和规范文件。
  • package.json: 项目的依赖和脚本配置文件。
  • tsconfig.json: TypeScript 配置文件。
  • webpack.config.js: Webpack 配置文件。

2. 项目的启动文件介绍

项目的启动文件是 src/index.tsx,它是整个项目的入口点。该文件主要负责导出 React-SVG 组件,使其可以在其他项目中使用。

import React from 'react'
import ReactDOM from 'react-dom'
import ReactSVG from './react-svg'

export default ReactSVG

3. 项目的配置文件介绍

package.json

package.json 文件包含了项目的依赖、脚本和其他元数据。以下是一些重要的部分:

{
  "name": "react-svg",
  "version": "11.1.6",
  "description": "A React component that injects SVG into the DOM.",
  "main": "dist/react-svg.js",
  "scripts": {
    "build": "npm run clean && npm run build:cjs && npm run build:es && npm run build:umd",
    "clean": "rimraf dist",
    "lint": "eslint src",
    "test": "jest"
  },
  "dependencies": {
    "@tanem/svg-injector": "^8.0.62",
    "prop-types": "^15.7.2"
  },
  "devDependencies": {
    "@babel/core": "^7.12.3",
    "@babel/preset-env": "^7.12.1",
    "@babel/preset-react": "^7.12.1",
    "@babel/preset-typescript": "^7.12.1",
    "eslint": "^7.12.1",
    "jest": "^26.6.1",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "typescript": "^4.0.3"
  }
}

tsconfig.json

tsconfig.json 文件是 TypeScript 的配置文件,定义了 TypeScript 编译器的选项。

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "es2015"],
    "jsx": "react",
    "module": "commonjs",
    "moduleResolution": "node",
    "out

react-svg:art: A React component that injects SVG into the DOM.项目地址:https://gitcode.com/gh_mirrors/re/react-svg

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用 `react-native-svg` 和 `react-native-svg-transformer` 加载本地 SVG 图像,你可以按照以下步骤进行操作: 1. 首先,确保你已经在项目中安装了 `react-native-svg` 和 `react-native-svg-transformer` 依赖。你可以通过运行以下命令来安装它们: ``` npm install react-native-svg react-native-svg-transformer ``` 2. 在项目的根目录下创建一个名为 `metro.config.js` 的文件(如果已存在,请跳过此步骤)。 3. 在 `metro.config.js` 文件中添加以下内容: ```javascript module.exports = { transformer: { assetPlugins: ['react-native-svg-transformer'], }, }; ``` 4. 接下来,在你的组件中,使用 `react-native-svg` 中的 `<SvgUri>` 组件来加载本地 SVG 图像。首先,确保你的 SVG 图像位于项目的 `assets` 文件夹中。 ```javascript import React from 'react'; import { View } from 'react-native'; import SvgUri from 'react-native-svg-uri'; const MyComponent = () => { return ( <View> <SvgUri width={200} height={200} source={require('./assets/myImage.svg')} /> </View> ); }; export default MyComponent; ``` 在上面的示例中,我们使用 `require` 方法加载位于 `assets` 文件夹中的 `myImage.svg` 图像,并将其作为 `source` 属性传递给 `<SvgUri>` 组件。你可以根据自己的需要调整宽度和高度。 5. 确保你在重新启动 Metro Bundler 之前完成了以上步骤。你可以通过运行以下命令重新启动 Metro Bundler: ``` npx react-native start --reset-cache ``` 这样,你就可以使用 `react-native-svg` 和 `react-native-svg-transformer` 成功加载和显示本地 SVG 图像了。 希望对你有所帮助!如有任何疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宗嫣惠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值