我有用函数组件编写的 SVG 文件。
import React from "react";
const MySVGBackground= (props) => {
return (
<svg> some svg here</svg>
);
};
export default MySVGBackground;
Run Code Online (Sandbox Code Playgroud)
我需要将其渲染为背景:
<div style={{ backgroundImage: `url(${MySVGBackground})` }}> </div>
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
我不能纯粹直接导入 SVG,因为它给了我一个 Unexpected Token 的错误。所以我必须将 SVG 包装到 FC 中并导出它。
这是示例代码:https : //codesandbox.io/s/react-background-issue-9wt4x?file=/src/App.js
reactjs ×1