我尝试将正确的类型添加到我的 d3.select 语句中,以便设置 mySVG 变量。
\nfunction App() {\n const [mySVG, setMySVG] = useState<d3.Selection<d3.BaseType, unknown, HTMLElement, any>>()\n const svgRef = useRef<SVGSVGElement | null>(null)\n\n useEffect(() => {\n setMySVG(d3.select(svgRef.current));\n \n }, [])\n\n return (\n <svg ref= {svgRef}>\n\n\n </svg>\n );\n}\nRun Code Online (Sandbox Code Playgroud)\n目前,我在代码中得到了下面的错误突出显示:
\nsetMySVG( d3.select(svgRef.current) );
\n\n\n选择指定的节点元素。
\n
\n\n第一个通用“GElement”指的是要选择的元素的类型。第二个通用“OldDatum”是指所选元素上的基准类型。当重新选择具有先前设置的已知基准类型的元素时,这非常有用。
\n
\n\n@param node \xe2\x80\x94 要选择的元素
\n
\n\n'Selection<SVGSVGElement | 类型的参数 null,unknown,null,undefined>' 不可分配给类型为 'SetStateAction<Selection<BaseType,unknown,HTMLElement,any> | 的参数 未定义>'。\n键入'选择<SVGSVGElement | null,unknown,null,undefined>' 不可分配给类型'Selection<BaseType,unknown,HTMLElement,any>'。
\n
\n'select(...).select(...).select(...).data' 的类型在这些类型之间不兼容。\n类型 '{ (): undefined[]; …
我有一个方法,让我来创建一个HashMap从ArrayList通过获取AirbnbListing对象,然后在附近的名称与任何键进行比较HashMap.如果它还没有在hashmap中,我将它添加一个从1开始的计数器,如果它已经存在,我增加计数器.
这里有一个更有效的方法是我的代码:
public HashMap<String, Integer> sortHousesInNbrhood(ArrayList<AirbnbListing> priceRangeListing) {
HashMap<String, Integer> housesInNbrhood = new HashMap<>();
for (AirbnbListing listing : priceRangeListing) {
if (housesInNbrhood.isEmpty()) {
housesInNbrhood.put(listing.getNeighbourhood(), 1);
} else if (housesInNbrhood.containsKey(listing.getNeighbourhood())) {
housesInNbrhood.replace(listing.getNeighbourhood(), housesInNbrhood.get(listing.getNeighbourhood()) + 1);
} else {
housesInNbrhood.put(listing.getNeighbourhood(),1);
}
}
return housesInNbrhood;
}
Run Code Online (Sandbox Code Playgroud) arraylist ×1
counter ×1
d3.js ×1
hashmap ×1
java ×1
javascript ×1
react-hooks ×1
reactjs ×1
sorting ×1
typescript ×1