小编Jos*_*ers的帖子

如何使用 SVG React 组件作为背景?

我有用函数组件编写的 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

2
推荐指数
1
解决办法
1927
查看次数

标签 统计

reactjs ×1