小编Log*_*nam的帖子

为了能够使用 useRef 钩子选择 SVG 元素,我应该指定 d3.select() 哪种打字稿类型?

我尝试将正确的类型添加到我的 d3.select 语句中,以便设置 mySVG 变量。

\n
function 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}\n
Run Code Online (Sandbox Code Playgroud)\n

目前,我在代码中得到了下面的错误突出显示:

\n

setMySVG( d3.select(svgRef.current) );

\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
\n

'select(...).select(...).select(...).data' 的类型在这些类型之间不兼容。\n类型 '{ (): undefined[]; …

javascript d3.js typescript reactjs react-hooks

9
推荐指数
1
解决办法
1277
查看次数

有没有更有效的方法使用ArrayList中的计数器来创建HashMap?

我有一个方法,让我来创建一个HashMapArrayList通过获取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)

java sorting counter arraylist hashmap

1
推荐指数
1
解决办法
159
查看次数