【问题标题】:React Native how to render svg from urlReact Native 如何从 url 渲染 svg
【发布时间】:2021-08-04 19:09:49
【问题描述】:

我正在尝试从 @dicebear/avatars 渲染 svg 文件路径

  let svg = createAvatar(style, {
    seed: 'random',
    // ... and other options
  });

svg 变量有这个值 =

但是当我尝试在 react-native 中渲染时,即使我使用了一个名为 react-native-render-html 的库,也没有显示 我正在尝试使用这样的库进行渲染

<View style={styles.container}>
  <HTML source={{ html: svg }} />

  <StatusBar style="white" />
</View>

当我这样做时,这就是我得到的[

【问题讨论】:

  • this 回答你的问题了吗?
  • 不,我没有 .svg 文件我只有 html svg 代码

标签: react-native svg


【解决方案1】:

您可以使用react-native-svg 执行此操作。 步骤:

  1. 使用npm install react-native-svg 安装库
  2. 清除缓存后重建应用程序。
  3. 现在像使用 XML 一样
import React from 'react';
import {createAvatar} from '@dicebear/avatars';
import * as style from '@dicebear/avatars-identicon-sprites';
import {SvgCss} from 'react-native-svg';

const xml = createAvatar(style, {
  seed: 'custom-seed',
});

export default function App() {
  return <SvgCss xml={xml} width="100%" height="100%" />;
}

【讨论】:

  • @Dawker 我认为您搞砸了任何组件的导入或导出。请确保您正确执行此操作,此错误与此库无关。
猜你喜欢
  • 1970-01-01
  • 2020-03-23
  • 2021-10-01
  • 2022-01-15
  • 2022-01-17
  • 2021-04-17
  • 2019-11-25
  • 2017-08-11
  • 1970-01-01
相关资源
最近更新 更多