【发布时间】:2023-03-14 05:03:01
【问题描述】:
我想按照教程添加一个 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;
【问题讨论】:
-
你说的什么都没有发生是什么意思?你的图表does work.