Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using ref using a function component gives a type error: Function components cannot be given refs. #668

Open
Robbie-Cook opened this issue Jan 23, 2025 · 0 comments

Comments

@Robbie-Cook
Copy link

Robbie-Cook commented Jan 23, 2025

Describe the bug

If you use a function component instead of a class component, there is a type error on the functional component., if you are using Typescript.
The error is the same as this one: expo/router#278

Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef() 

To Reproduce

Steps to reproduce the behavior:

Use component like so:

import EditorComponent, {
  DiffEditor,
  useMonaco,
  loader,
} from '@monaco-editor/react'
import { useEffect, useRef } from 'react'

export function Editor() {
  const editorRef = useRef<typeof EditorComponent>(null)

  useEffect(() => {
    editorRef.current?.getValue()
  })

  return (
    <EditorComponent
      ref={editorRef}
      height="90vh"
      defaultLanguage="typescriptreact"
      defaultValue="// some comment"
      theme="dark"
    />
  )
}
@Robbie-Cook Robbie-Cook changed the title Using ref using a function component gives an error Using ref using a function component gives an error: Function components cannot be given refs. Jan 23, 2025
@Robbie-Cook Robbie-Cook changed the title Using ref using a function component gives an error: Function components cannot be given refs. Using ref using a function component gives a type error: Function components cannot be given refs. Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant