【发布时间】:2020-11-20 03:17:05
【问题描述】:
我有这个 SVG sprite 的代码
<symbol xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 22 22" id="github">
<defs>
<pattern id="a" preserveAspectRatio="none" width="100%" height="100%" viewBox="0 0 436 428">
<image width="436" height="428" xlink:href="data:image/png;base64,.........."></image>
</pattern>
</defs>
<circle cx="11" cy="11" r="11" fill="#fff"></circle>
<path fill="url(#a)" d="M0 0h22v22H0z"></path>
</symbol>
GithHub 图标用 HTML 代码完美呈现,
<svg class="icon">
<use xlink:href="#github"></use>
</svg>
当我使用 React.JS 时,图标没有显示出来。
icons2.svg
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<symbol xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 22 22" id="github">
<defs>
<pattern id="a" preserveAspectRatio="none" width="100%" height="100%" viewBox="0 0 436 428">
<image width="436" height="428" xlink:href="data:image/png;base64,..."></image>
</pattern>
</defs>
<circle cx="11" cy="11" r="11" fill="#fff"></circle>
<path fill="url(#a)" d="M0 0h22v22H0z"></path>
</symbol>
</defs>
</svg>
App.tsx
import icons from './icons2.svg';
let five = 'github';
class App extends Component {
render() {
return (
<div>
<svg>
<use href={`${icons}#${five}`} />
</svg>
</div>
);
}
我在这里做错了什么?
【问题讨论】:
-
你能澄清一下吗?
-
它没有用
-
具有硬编码 id 值的组件是footgun。