javascript - koju vrstu dati ref u React/TypeScript prilikom korištenja React.forwardRef?


Jednostavno ne mogu ispravno postaviti tipove. Prosljeđujem referencu koja radi ispravno ako postavim ref: any kao što je prikazano u nastavku i ref.current.focus() radi.

function EditorFeat(): JSX.Element {
  const ref = useRef<HTMLElement | null>(null);

   // ...
   return (
    ......
    <Editor state={state} dispatch={dispatch} ref={ref} />
     ....
)
}

export default EditorFeat;
interface IEditor {
  state: AppState;
  dispatch: React.Dispatch<Action>;
}

// change ref:any to correct typings!
function Editor({ state, dispatch }: IEditor, ref: any): JSX.Element {
// ...
  return <DraftJsEditor ref={ref} /> // import from 'draft-js'
}

export default React.forwardRef(Editor);

ali čim pokušam ispravno dobiti ref tip, stalno dobivam pogreške!

ako napravim function Editor({state, dispatch}: IEditor, ref: HTMLElement): JSX.Element

Dobivam sljedeću pogrešku:

  Overload 1 of 2, '(props: Readonly<DraftEditorProps>): DraftEditor', gave the following error.
    Type 'HTMLElement' is not assignable to type 'string | ((instance: DraftEditor | null) => void) | RefObject<DraftEditor> | null | undefined'.
      Type 'HTMLElement' is not assignable to type 'string'.
  Overload 2 of 2, '(props: DraftEditorProps, context?: any): DraftEditor', gave the following error.
    Type 'HTMLElement' is not assignable to type 'string | ((instance: DraftEditor | null) => void) | RefObject<DraftEditor> | null | undefined'.
      Type 'HTMLElement' is not assignable to type 'string'

ako pokušam React.RefObject<DraftEditor> dobijem:

Argument of type '({ state, dispatch }: IEditor, ref: RefObject<DraftEditor>) => Element' is not assignable to parameter of type 'ForwardRefRenderFunction<DraftEditor, IEditor>'.
  Types of parameters 'ref' and 'ref' are incompatible.
    Type '((instance: DraftEditor | null) => void) | MutableRefObject<DraftEditor | null> | null' is not assignable to type 'RefObject<DraftEditor>'.
      Type 'null' is not assignable to type 'RefObject<DraftEditor>'

Odgovor #1

S funkcionalnom komponentom ovo radi kao šarm, iako je malo neobično:

function RichEditor(props: RichEditorProps) {
  const [editor, setEditor] = React.useState<Editor | null>(null);

  React.useEffect(() => {
    editor?.focus() // or anywhere you want to use it
  }, [editor])
    

  return (
    <Editor
      ref={editor => setEditor(editor)}
    />
  )
}

Odgovor #2

Ako napišete React.forwardRef izravno na vrhu komponente, a ne u liniji return naredbe, morate samo upisati React.forwardRef:

interface IEditor {
  state: AppState;
  dispatch: React.Dispatch<Action>;
}

const Editor = React.forwardRef<HTMLInputElement, IEditor>({ state, dispatch }, ref) {
  // ...
  return <DraftJsEditor ref={ref} /> // import from 'draft-js'
}

export default Editor;

Objašnjenje: Upisujete React.forwardRef s dva generička tipa parametara T i P:

React.forwardRef<T, P>(Editor)
  • T je tip HTML elementa na koji se na kraju pozivate preko DraftJsEditor, pa to može biti HTMLDivElement ili HTMLInputElement ili bilo koji drugi element. Poruka o grešci u TypeScriptu obično će vas uputiti na pravi kada prvo jednostavno odaberete bilo koji pogrešan.
  • P su svojstva Editor komponente.

Dakle, u vašem slučaju to bi moglo biti React.forwardRef<HTMLInputElement, IEditor>(Editor) kao što možete vidjeti u velikom rješenju gore.