我想添加一个d3图表,按照教程操作但是没有任何反应。我不确定useEffect()是否在正确的"时机"上使用,是否应该使用componentDidMount,或者是否添加元素的方法不正确...看起来我在这里漏了些什么!
import React from 'react';
import * as d3 from "d3";
import { useEffect } from 'react';
function drawChart() {
const data = [12, 5, 6, 6, 9, 10];
const h = 100;
const w = 100;
const svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h)
.style("margin-left", 100);
svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", (d, i) => i * 70)
.attr("y", (d, i) => h - 10 * d)
.attr("width", 65)
.attr("height", (d, i) => d * 10)
.attr("fill", "green")
}
const chart: React.FunctionComponent = () => {
useEffect(() => {
drawChart();
}, []);
return (
<div>
</div>
);
};
export default chart;