【发布时间】:2020-04-21 02:04:40
【问题描述】:
我有用函数组件编写的 SVG 文件。
import React from "react";
const MySVGBackground= (props) => {
return (
<svg> some svg here</svg>
);
};
export default MySVGBackground;
我需要将此渲染为背景:
<div style={{ backgroundImage: `url(${MySVGBackground})` }}> </div>
但它不起作用。
我不能直接直接导入 SVG,因为它会给我一个 Unexpected Token 的错误。所以我必须将 SVG 包装到 FC 中并导出。
下面是示例代码: https://codesandbox.io/s/react-background-issue-9wt4x?file=/src/App.js
【问题讨论】:
-
您的 svg 需要动态吗?如果是这样,您是否打算将道具传递给它?
-
静态,不传递任何道具。
标签: reactjs