最简单的方法是什么?我发现了很多svg到JSX的转换器,这正是我想要的,但在react native中不起作用.我需要将svg代码转换为可以使用react native svg在我的应用程序中显示的内容.谢谢

推荐答案

我可以想到以下几种 Select .您使用的版本取决于需要转换的文件数量.

Option 1(几乎没有文件)

将svg代码复制粘贴到site上,并选中React Native复选框.这将为您提供可用于react-native-svg的代码

在以下代码中使用该输出(用生成的内容替换SvgComponent):

import React, { Component } from 'react';
import { View } from 'react-native';
import Svg, { Circle, Path } from 'react-native-svg';

const SvgComponent = props => (
  <Svg viewBox="0 0 48 1" style={props.style}>
    <Path d="M0 0h48v1H0z" fill="#063855" fillRule="evenodd" />
  </Svg>
);

class MySVGIcon extends Component {
  render() {
    const { style } = this.props;
    const component = SvgComponent(style);

    return <View style={style}>{component}</View>;
  }
}

export default MySVGIcon;

Option 2(许多文件)

不需要转换它们,您可以使用react-native-remote-svg将它们直接嵌入到代码中.

例如,你可以这样做:

import Image from 'react-native-remote-svg';

class MyImageClass extends Component {

  render () {
    // Embed code or read a SVG file...
    const mySVGImage = '<svg width="48px" height="1px" viewBox="0 0 48 1" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><rect id="Rectangle-5" x="25" y="36" width="48" height="1"></rect></svg>';

    return (
        <Image source={{
                          uri: "data:image/svg+xml;utf8," + mySVGImage
                      }}/>
    );
  }
}

Option 3(最酷的选项)

它不像其他两个选项那样免费,但也不贵.PaintCode是我最喜欢的工具之一.版本3+现在支持java脚本.因此,你可以在其中加载svg文件,它会输出你可以在应用程序中使用的代码.这里的优点是界面比上面的第一个选项更友好,然后可以从代码中为svg文件中的每个对象设置动画.

React-native相关问答推荐

莫迪德不会停留在屏幕中央

为什么当我更换屏幕时playground 音乐不会停止?

无法读取 @react-native-async-storage/async-storage 的未定义属性setItem

在 react native 平面列表中动态定位弹出窗口

'React native run android' 在模拟器中启动应用程序后立即停止

react-native 中的layout-only view removal优化是什么?

在 iOS 模拟器中运行 React Native 应用程序时找不到 UMModuleRegistryAdapter.h

Visual Studio 代码错误:-Failed to start flow Error: Wrong version of Flow. The config specifies version ^0.92.0 but this is version 0.95.1

react native 中 componentDidMount 和 componentDidUpdate 有什么区别

react-native android中出现此问题Cannot convert argument of type class org.json.JSONArray的原因是什么?

如何在 React Native 中本地存储不是字符串的数据

命令失败:gradlew.bat installDebug error whenever installing dependencies like navigation, firebase, icons etc in React-Native

在使用 redux-persist 重新水化存储之前加载初始状态的默认值

React Native ScrollView 在snapping后重新回到顶部

在 react-native-maps 中渲染多个标记

Animated.View 的样式props的Typescript定义

React Native 无法运行堆栈跟踪

React Native 发送短信

React Native 将状态中的数组值应用为 Picker 项

为什么 this.state 在react-native中未定义?