【发布时间】:2022-01-09 08:23:29
【问题描述】:
如何以编程方式创建在 React 组件中呈现的 SVG 形状?在本例中,我创建了一个包含 20 个点的字符串。但是,将{dots} 放在标签<svg> 和</svg> 之间不起作用。只需将{dots} 放在组件的返回函数中,它就会显示为一个字符串。
我做错了什么?任何帮助和建议都非常感谢!!!
import './SVGLayer.css';
import React from 'react';
const SVGLayer = () => {
let dots: string = '';
for (let i = 0; i < 20; i++) {
dots += `<circle cx="${100 + i * 10}px" cy="100px" r="0.5px" stroke="black" />`;
}
return (
<div className="svg-layer">
<svg
xmlns="http://www.w3.org/2000/svg"
version="1.1"
width="800px"
height="600px"
>
<circle cx="100px" cy="100px" r="1px" stroke="black" />
{dots}
</svg>
</div>
);
};
export default SVGLayer;
【问题讨论】: