0

我正在尝试在我的反应项目中实现 SignaturePad,我可以绘制签名,但我无法访问属性以清除它或保存它。我在 ref={signCanvas} 以及 current.clear() 和 signCanvas.current.clear() 和 signCanvas.getTrimmedCanvas().toDataURL("image/png") 上收到以下错误

没有重载匹配此调用。

  Overload 1 of 2, '(props: Readonly<ReactSignatureCanvasProps>): ReactSignatureCanvas', gave the following error.
    Type 'MutableRefObject<{}>' is not assignable to type 'string | ((instance: ReactSignatureCanvas | null) => void) | RefObject<ReactSignatureCanvas> | null | undefined'.
      Type 'MutableRefObject<{}>' is not assignable to type 'RefObject<ReactSignatureCanvas>'.
        Types of property 'current' are incompatible.
          Type '{}' is missing the following properties from type 'ReactSignatureCanvas': on, off, clear, isEmpty, and 14 more.
  Overload 2 of 2, '(props: ReactSignatureCanvasProps, context?: any): ReactSignatureCanvas', gave the following error.
    Type 'MutableRefObject<{}>' is not assignable to type 'string | ((instance: ReactSignatureCanvas | null) => void) | RefObject<ReactSignatureCanvas> | null | undefined'.
      Type 'MutableRefObject<{}>' is not assignable to type 'RefObject<ReactSignatureCanvas>'.ts(2769)
index.d.ts(143, 9): The expected type comes from property 'ref' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<ReactSignatureCanvas> & Readonly<ReactSignatureCanvasProps> & Readonly<...>'
index.d.ts(143, 9): The expected type comes from property 'ref' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<ReactSignatureCanvas> & Readonly<ReactSignatureCanvasProps> & Readonly<...>'*

这是我迄今为止实施的,任何帮助将不胜感激。

const signCanvas = useRef({});

const clear = () => signCanvas.current.clear();
const save = () => console.log(signCanvas.getTrimmedCanvas().toDataURL("image/png"));

<SignaturePad
 ref={signCanvas}
 canvasProps={{ className: "signature__canvas" }}
/>

<Button color='grey' content='Clear' onClick={clear} />
<Button color='grey' content='Save' onClick={save} />
4

1 回答 1

2

我设法解决了这个问题。如果有人有同样的问题,只需为 React.MutableRefObject 添加以下内容

const signCanvas = React.useRef() as React.MutableRefObject<any>;

并且以下现在工作正常。

 const clear = () => signCanvas.current.clear();

 const save = () =>
 console.log(signCanvas.current.getTrimmedCanvas().toDataURL("image/png"));
于 2020-12-11T11:01:01.797 回答