【发布时间】: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>
);
}
这什么都不渲染,我该如何渲染这个字符串,或者我应该把字符串变成一个数组,然后把它渲染成一个数组?
【问题讨论】:
-
为什么要直接声明为字符串而不是 React 元素?