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

241
Views
How can I use forwardRef() in React?

I'm currently receiving the following error in my React app:

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

How can I fix this using forwardRef()?

My code is as follows:

const Services: FunctionComponent = (): ReactElement => {
  const servicesRef = useRef(null);

  return (
    <Layout>
      <ServicesList ref={servicesRef} />
    </Layout>
  );
};
export default Services;



const ServicesList: React.FunctionComponent = ({ children }: Props) => {
  return (
    <section className="my-24 md:my-32">
      {children && children}
    </section>
  );
};

export default ServicesList;
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

function Parent(){      
   
   const childRef = React.useRef(null)

   // do something with your childRef
   React.useEffect(()=> { childREf.current.focus() },[])

   
   return( <div>  <Child ref={childRef} /> </div>)
}

const Child = React.forwardRef((props, ref) => {

   console.log(ref.current)   

   return( <div tabIndex={1} ref={ref}> child </div)
})
over 4 years ago · Santiago Trujillo Report

0

React docs explain well..

https://reactjs.org/docs/forwarding-refs.html

Wrap your component with React.forwardRef and set the ref to the intended DOM element

 const FancyButton = React.forwardRef((props,ref)=>(
      <buttonref={ref} className="FancyButton">
         {props.children}
      </button>)
   );
over 4 years ago · Santiago Trujillo 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!