【问题标题】:How to convert string to jsx in react native如何在本机反应中将字符串转换为jsx
【发布时间】:2018-06-29 19:59:50
【问题描述】:

我正在尝试将 javascript 字符串显示为 jsx,似乎无法在 react native 中将字符串显示为 jsx,例如const test = '<Text>hello</Text>'render() { return ({test})}.

给定以下字符串

const svg = '<Svg.G fill="none" fill-rule="evenodd"> <Svg.Path fill="none" d="M0 0h24v24H0z"/> <Svg.Path stroke="#333" stroke-linecap="round" stroke-linejoin="round" d="M3.5 12.5h17v9h-17zM13.5 12.5v9M10.5 12.5v9M1.883 9.602l18.353-4.918.776 2.898L2.66 12.5z"/> <Svg.Path stroke="#333" stroke-linejoin="round" d="M6 6.857c.957.553 4.675.393 4.675.393S8.957 3.945 8 3.393a2 2 0 1 0-2 3.465zM15.296 4.366c-.546.956-3.852 2.674-3.852 2.674s-.164-3.718.388-4.674a2 2 0 1 1 3.464 2z"/> <Svg.Path stroke="#333" stroke-linecap="round" stroke-linejoin="round" d="M12.508 6.755l.777 2.897M9.61 7.531l.776 2.899"/></Svg.G>';

然后我想像下面这样渲染它

render() {
    return (
      <View>
        <Svg>
          {svg}
        </Svg>
      </View>
    );
}

这什么都不渲染,我该如何渲染这个字符串,或者我应该把字符串变成一个数组,然后把它渲染成一个数组?

【问题讨论】:

标签: javascript react-native


【解决方案1】:

这不是您问题的答案,但也许它可以帮助您弄清楚如何做您想做的事。 我强烈不推荐使用这种方法;)

所以你不能只对这个字符串使用 eval,因为你需要将 jsx 转换为 js。 Babel 在构建阶段为我们做这件事,但你可以解析一个字符串并将其转换为 React.createElement 形状。

只有当你知道你收到的组件的字符串格式是什么时它才会起作用

我仅为您的示例 (&lt;Text&gt;hello&lt;/Text&gt;) 提供示例,因为它更容易,但它会帮助您获得一个想法。

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

function mapStringToComponent(stringToRender) {
    const parseResult = stringToRender.match(/<([a-z]*)>(.*)<\/[a-z]*>/i);
     // result of this regex ["<Text>hello</Text>", "Text", "hello"]

    if (parseResult !== null) {
      const [, compName, innerText] = parseResult;

      return React.createElement(
        RN[compName],
        null, // here may be an object with attributes if your node has any
        innerText,
      );
    }

    return null;
}


export default class MyDynamicComponent extends PureComponent {
  render() {
    const component = mapStringToComponent('<Text>hello</Text>');

    return (
      <View>
       {component}
      </View>
    );
  };
}

您可以在此处查看转换后的 svg 字符串的外观:babel link

我尝试用谷歌搜索一些可以为你做的库,但没有找到。也许我不擅长谷歌搜索:)

【讨论】:

  • 这看起来可以工作,但是我们使用的 react-native 版本不再有默认导出。所以 babel 在import RN ... 上窒息了,有没有可能对此进行更新?我还没有找到解决方法。
【解决方案2】:

我们可以使用 import react-jsx-parser 来做到这一点 添加了一行来过滤掉行和空格,因为当它们在 SVG 代码中是空格时,解析器会抛出错误

例如。

    var svg = `<Svg
    width={286}
    height={335}
    viewBox="0 0 286 335"
    fill="none"
    xmlns="http://www.w3.org/2000/svg"
    {...props}
    >
    <Path
      d="M63.763 25.066h17.72L107.542 1l14.593 16.741L248.258 32.39V140.16L284.74 257.35c-3.822 3.488-20.013 15.486-54.202 35.575-51.7 25.112-174.419 37.668-229.315 40.807l11.465-193.571c5.56.698 23.557 1.675 51.075 0 34.398-2.092 37.525 2.093 43.779-15.694 6.254-17.788-6.254-35.576-13.55-49.178-5.838-10.882-22.585-14.997-30.229-15.695V25.065z"
      fill="#F8F9F8"
      stroke="#22223B"
      strokeOpacity={0.8}
      strokeWidth={1.06988}
      strokeLinecap="round"
      strokeDasharray="6.42 6.42"
    />
    <Path
    onPress={handleOnPress}
      transform="rotate(3.757 16.59 146.446)"
      fill="#9A8C98"
      fillOpacity={0.22}
      d="M16.5894 146.446H40.5513V192.058H16.5894z"
    />
  </Svg>`
    svg = svg.replace(/\n/g, ' ').replace(/\>[\t ]+\</g, "><");
    return (
        <JsxParser
            components={{ Svg, Path }}
            renderInWrapper={false}
            bindings={{
                foo: 'foo',
                handleOnPress: () => alert('sasas'),
            }}
            blacklistedAttrs={[]} // this line should fix your issue
            jsx={svg}
        />
    );

【讨论】:

    【解决方案3】:

    我保存了你试图显示为 svg 文件的 svg const 并打开它,但它是空白的

    所以尝试修复它

    在 react native 中显示 svg 的最佳方式是这种方式

    import Image from 'react-native-remote-svg';
    <View>
        <Image source={require('./assets/picture.svg')} />
    </View>
    

    【讨论】:

    猜你喜欢
    • 2017-07-23
    • 2020-04-25
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2022-10-17
    相关资源
    最近更新 更多