Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
Getting Property 'ref' does not exist on type 'IntrinsicAttributes' error

I'm implementing a link in React and TypesSript that, after clicking, scrolls to the right component, I'm using useRef Hook.

But when I do that, the error occurs:

Type '{ ref: MutableRefObject<HTMLDivElement | null>; }' is not assignable to type 'IntrinsicAttributes'.
  Property 'ref' does not exist on type 'IntrinsicAttributes'.

Below is the code, what am I doing wrong? I've tried several ways.

  import NewComponent from '../NewComponent';

  const titleRef = useRef<null | HTMLDivElement>(null);

  const handleBackClick = (): any => {
    titleRef!.current!.scrollIntoView({ behavior: 'smooth' });
  };

return (
  <Container>
    <Link onClick={handleBackClick}>
      Button Text
    </Link>
     ...
     ...
     ...
     <SomeComponents />
     ...
     ...
     ...
     ...

   <NewComponent ref={titleRef} />
  </Container>
)

The NewComponent is a simple component, like:

const NewComponent = () => {
  
    return (
      <Container>
        // Its a simple component
      </Container>
    );
  };
  
  export default NewComponent;

Thanks to anyone who can help me with this!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You would wanna use forwardRef to tell TypeScript that NewComponent can accept a ref that will hold a div element. Here is how you would do it as an example:

import { forwardRef } from "react";
const NewComponent = forwardRef<HTMLDivElement>((props, ref) => {
  return <div ref={ref}>{props.children}</div>;
});
import { useRef } from "react";
export default function App() {
  const titleRef = useRef<HTMLDivElement>(null);
  return (
    <div>
      <NewComponent ref={titleRef} />
    </div>
  );
}

I created this CodeSandbox if you wanna play with it. Also notice I change useRef<null | HTMLDivElement>(null) to useRef<HTMLDivElement>(null).

about 4 years ago · Juan Pablo Isaza Report

0

In React, ref can only attach to DOM, if you want to pass it to your react component, you should use forwardRef

you can find more detail here https://reactjs.org/docs/react-api.html#reactforwardref

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!