繁体   English   中英

如何在 React Native 中使用 SVG?

[英]How can I use SVG in React Native?

我正在尝试在本机反应中使用引导图标,但我找不到任何有用的方法来说明如何在 react-native 中渲染 SVG。 有谁知道怎么做?

要在 react native 中添加 SVG,您可以使用react-native-svg

安装:

纱线添加反应原生SVG

例子:

import Svg, {
  Circle,
  Ellipse,
  G,
  Text,
  TSpan,
  TextPath,
  Path,
  Polygon,
  Polyline,
  Line,
  Rect,
  Use,
  Image,
  Symbol,
  Defs,
  LinearGradient,
  RadialGradient,
  Stop,
  ClipPath,
  Pattern,
  Mask,
} from 'react-native-svg';

/* Use this if you are using Expo
import * as Svg from 'react-native-svg';
const { Circle, Rect } = Svg;
*/

import React from 'react';
import { View, StyleSheet } from 'react-native';

export default class SvgExample extends React.Component {
  render() {
    return (
      <View
        style={[
          StyleSheet.absoluteFill,
          { alignItems: 'center', justifyContent: 'center' },
        ]}
      >
        <Svg height="50%" width="50%" viewBox="0 0 100 100">
          <Circle
            cx="50"
            cy="50"
            r="45"
            stroke="blue"
            strokeWidth="2.5"
            fill="green"
          />
          <Rect
            x="15"
            y="15"
            width="70"
            height="70"
            stroke="red"
            strokeWidth="2"
            fill="yellow"
          />
        </Svg>
      </View>
    );
  }
}

或者您想将 svg 导入您的应用程序使用: react-native-svg-transformer

例子:

import Logo from "./logo.svg";

<Logo width={120} height={40} />

但是如果你想要一个图标库,你可以使用: react-native-vector-icons

这里也是目录: React Native Vector Icons directory

我刚刚找到了一个简单的方法来解决这个对我有用的问题。

我正在使用 Expo 并安装了react-native-svg 请注意,这与从react-native-svg-uri导入SvgUri不同。 那是一个不同的图书馆。

import { SvgUri } from "react-native-svg";
<SvgUri
  uri="https://example.com/uploads/apple.svg"
  width="40%"
  height="40%"
/>

推荐包裹

const logo = new URL('logo.svg', import.meta.url);
export function Logo() {return <img src={logo} alt="logo" />}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM