简体   繁体   English

参考是 null Typescript + NextJS

[英]ref is null Typescript + NextJS

I need to call methods from a custom child component inside the parent component.我需要从父组件内的自定义子组件调用方法。

But unfortunately the ref to the child component (called CanvasUI) is always null.但不幸的是,子组件(称为 CanvasUI)的引用始终是 null。 I don't understand why as it seems to me that I have implemented everything correctly.我不明白为什么在我看来我已经正确地实现了一切。

This is my parent component这是我的父组件

...

export default function Home() {
  ...
  const canvas = useRef<CanvasRef>(null);

  ...
  function clearCanvas() {
    // canvas.current.resetCanvas();
    console.log(canvas.current);
  }
  return (
    <div className="flex justify-center items-center min-h-screen min-w-screen bg-main">
      <div className="flex flex-col">
        ...
        <div className="flex justify-center">
          ...
          <CanvasUI disabled={disableCanvas} word={activeWord} ref={canvas} />
          ...
        </div>
        ...
        <button onClick={() => clearCanvas()}>clear canvas</button>
      </div>
    </div>
  );
}

And this is the CanvasUI component这是 CanvasUI 组件

...

export default function CanvasUI({
  disabled,
  word,
  ref,
}: {
  disabled: boolean;
  word: string;
  ref: Ref<CanvasRef>;
}) {
  ...
  useImperativeHandle(ref, () => ({ getCanvas, loadCanvas, resetCanvas }));

  ...

  function resetCanvas(): void {
    ...
  }

  ...

  function getCanvas(): String {
    ...
  }

  function loadCanvas(data: String, immediate: Boolean): void {
    ...
  }

  return (
    ...
  );
}

CanvasRef Interface CanvasRef 接口

export default interface CanvasRef {
  getCanvas: () => String;
  loadCanvas: (data: String, immediate: Boolean) => void;
  resetCanvas: () => void;
}

I left out unimportant code to make it more readable我省略了不重要的代码以使其更具可读性

You can't use ref as a prop in a component because it is reserved (just like key ).您不能将ref用作组件中的道具,因为它是保留的(就像key一样)。 React will omit the ref prop from props when invoking your function component.在调用 function 组件时,React 将省略props中的ref属性。

Instead, you should position ref as the second parameter to your CanvasUI component, and then create it using forwardRef .相反,您应该将 position ref作为CanvasUI组件的第二个参数,然后使用forwardRef创建它。 This is explained in the documentation for useImperativeHandle :这在useImperativeHandle的文档中进行了解释:

useImperativeHandle使用命令式句柄

useImperativeHandle(ref, createHandle, [deps])

useImperativeHandle customizes the instance value that is exposed to parent components when using ref . useImperativeHandle自定义使用ref时暴露给父组件的实例值。 As always, imperative code using refs should be avoided in most cases.与往常一样,在大多数情况下应避免使用 refs 的命令式代码。 useImperativeHandle should be used with forwardRef : useImperativeHandle应该与forwardRef一起使用:

 function FancyInput(props, ref) { const inputRef = useRef(); useImperativeHandle(ref, () => ({ focus: () => { inputRef.current.focus(); } })); return <input ref={inputRef}... />; } FancyInput = forwardRef(FancyInput);

In this example, a parent component that renders <FancyInput ref={inputRef} /> would be able to call inputRef.current.focus() .在此示例中,呈现<FancyInput ref={inputRef} />的父组件将能够调用inputRef.current.focus()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM