【发布时间】:2020-11-17 01:10:39
【问题描述】:
我有以下代码;
App.tsx
export default function App() {
const [canvasRef, canvasWidth, canvasHeight] = useCanvas();
return (
<div>
<canvas
ref={canvasRef}
/>
</div>
)
}
useCanvas.ts
import { useRef, useEffect } from "react";
const canvasWidth = 1200;
const canvasHeight = 600;
export default function useCanvas() {
const canvasRef = useRef<HTMLCanvasElement>(null);
return [canvasRef, canvasWidth, canvasHeight]
}
是的,这正是我的代码中的内容,但是,我在来自 Vscode 的 App.tsx 中收到以下错误;
(JSX attribute) React.ClassAttributes<HTMLCanvasElement>.ref?: string | React.RefObject<HTMLCanvasElement> | ((instance: HTMLCanvasElement | null) => void) | null | undefined
Type 'number | RefObject<HTMLCanvasElement>' is not assignable to type 'string | RefObject<HTMLCanvasElement> | ((instance: HTMLCanvasElement | null) => void) | null | undefined'.
Type 'number' is not assignable to type 'string | RefObject<HTMLCanvasElement> | ((instance: HTMLCanvasElement | null) => void) | null | undefined'.ts(2322)
index.d.ts(143, 9): The expected type comes from property 'ref' which is declared here on type 'DetailedHTMLProps<CanvasHTMLAttributes<HTMLCanvasElement>, HTMLCanvasElement>'
任何建议将不胜感激。
【问题讨论】:
-
检查类似的问题,这可能会帮助你stackoverflow.com/questions/33796267/…
标签: javascript reactjs typescript