How to assign refs created from useRef to child components in React Typescript?

So I have typescript react project where I want to observe certain child components being viewed, to achieve this I am using IntersectionObserver and refs. In my parent component I have an introduction component imported from another file and I am trying to pass introRef into the component to be used to reference elements in Introduction

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>introRef = useRef(null)
...
<Introduction ref={introRef} />
</code>
<code>introRef = useRef(null) ... <Introduction ref={introRef} /> </code>
introRef = useRef(null)
...
<Introduction ref={introRef} />

My Introduction component is defined as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import React, { forwardRef, LegacyRef } from "react";
import myInfo from "../myInfo.json";
const Introduction = forwardRef<HTMLElement, React.HTMLProps<HTMLElement>>((ref) => {
return (
<section ref={ref} className="Introduction">
<p className="greeting">Welcome</p>
<p className="intro">{myInfo.introduction}</p>
<div className="picture">
{/* <PixelateToUnpixelate src={myInfo.myPicture} /> */}
</div>
</section>
);
});
export default Introduction;
</code>
<code>import React, { forwardRef, LegacyRef } from "react"; import myInfo from "../myInfo.json"; const Introduction = forwardRef<HTMLElement, React.HTMLProps<HTMLElement>>((ref) => { return ( <section ref={ref} className="Introduction"> <p className="greeting">Welcome</p> <p className="intro">{myInfo.introduction}</p> <div className="picture"> {/* <PixelateToUnpixelate src={myInfo.myPicture} /> */} </div> </section> ); }); export default Introduction; </code>
import React, { forwardRef, LegacyRef } from "react";
import myInfo from "../myInfo.json";


const Introduction = forwardRef<HTMLElement, React.HTMLProps<HTMLElement>>((ref) => {
    return (
      <section ref={ref} className="Introduction">  
          <p className="greeting">Welcome</p>
          <p className="intro">{myInfo.introduction}</p>
          <div className="picture">
          {/* <PixelateToUnpixelate src={myInfo.myPicture} /> */}
          </div>
      </section>
    );
  });
  
  export default Introduction;

on the line : <section ref={ref} className="Introduction"> I am getting the error:
Type ‘HTMLProps’ is not assignable to type ‘LegacyRef | undefined’

To combat the issue I have tried changing the declaration of Introduction to the following

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const Introduction = forwardRef((ref:LegacyRef<HTMLElement>) => {
return (
<section ref={ref} className="Introduction">
<p className="greeting">Welcome</p>
<p className="intro">{myInfo.introduction}</p>
<div className="picture">
{/* <PixelateToUnpixelate src={myInfo.myPicture} /> */}
</div>
</section>
);
});
</code>
<code>const Introduction = forwardRef((ref:LegacyRef<HTMLElement>) => { return ( <section ref={ref} className="Introduction"> <p className="greeting">Welcome</p> <p className="intro">{myInfo.introduction}</p> <div className="picture"> {/* <PixelateToUnpixelate src={myInfo.myPicture} /> */} </div> </section> ); }); </code>
const Introduction = forwardRef((ref:LegacyRef<HTMLElement>) => {
    return (
      <section ref={ref} className="Introduction">  
          <p className="greeting">Welcome</p>
          <p className="intro">{myInfo.introduction}</p>
          <div className="picture">
          {/* <PixelateToUnpixelate src={myInfo.myPicture} /> */}
          </div>
      </section>
    );
  });

The issue here disappeared, but when I try to use the Introduction component in the parent component and pass pass introRef into Introduction I am getting the error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Type '{ ref: MutableRefObject<null>; }' is not assignable to type 'IntrinsicAttributes & (LegacyRef<HTMLElement> & RefAttributes<unknown>)'.
Property 'current' is missing in type '{ ref: MutableRefObject<null>; }' but required in type 'RefObject<HTMLElement>'
</code>
<code>Type '{ ref: MutableRefObject<null>; }' is not assignable to type 'IntrinsicAttributes & (LegacyRef<HTMLElement> & RefAttributes<unknown>)'. Property 'current' is missing in type '{ ref: MutableRefObject<null>; }' but required in type 'RefObject<HTMLElement>' </code>
Type '{ ref: MutableRefObject<null>; }' is not assignable to type 'IntrinsicAttributes & (LegacyRef<HTMLElement> & RefAttributes<unknown>)'.
  Property 'current' is missing in type '{ ref: MutableRefObject<null>; }' but required in type 'RefObject<HTMLElement>'

So again I changed my Introduction component code to:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const Introduction = forwardRef((ref:React.MutableRefObject<null>) => {
return (
<section ref={ref} className="Introduction">
<p className="greeting">Welcome</p>
<p className="intro">{myInfo.introduction}</p>
<div className="picture">
{/* <PixelateToUnpixelate src={myInfo.myPicture} /> */}
</div>
</section>
);
});
</code>
<code>const Introduction = forwardRef((ref:React.MutableRefObject<null>) => { return ( <section ref={ref} className="Introduction"> <p className="greeting">Welcome</p> <p className="intro">{myInfo.introduction}</p> <div className="picture"> {/* <PixelateToUnpixelate src={myInfo.myPicture} /> */} </div> </section> ); }); </code>
const Introduction = forwardRef((ref:React.MutableRefObject<null>) => {
    return (
      <section ref={ref} className="Introduction">  
          <p className="greeting">Welcome</p>
          <p className="intro">{myInfo.introduction}</p>
          <div className="picture">
          {/* <PixelateToUnpixelate src={myInfo.myPicture} /> */}
          </div>
      </section>
    );
  });

Now, I am getting this error when I try to pass introRef into Introduction:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Property 'current' is missing in type '{ ref: MutableRefObject<null>; }' but required in type 'MutableRefObject<null>'.
</code>
<code>Property 'current' is missing in type '{ ref: MutableRefObject<null>; }' but required in type 'MutableRefObject<null>'. </code>
Property 'current' is missing in type '{ ref: MutableRefObject<null>; }' but required in type 'MutableRefObject<null>'.

I am out of ideas on how to solve this as from the documentation from React useRef:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>current: Initially, it’s set to the initialValue you have passed. You can later set it to something else. If you pass the ref object to React as a ref attribute to a JSX node, React will set its current property.
</code>
<code>current: Initially, it’s set to the initialValue you have passed. You can later set it to something else. If you pass the ref object to React as a ref attribute to a JSX node, React will set its current property. </code>
current: Initially, it’s set to the initialValue you have passed. You can later set it to something else. If you pass the ref object to React as a ref attribute to a JSX node, React will set its current property.

So I can’t possibly have the current property if I don’t pass the ref object to a ref attribute to a JSX node, in this case Introduction.

I have also tried doing this without wrapping Introduction in forwardRef, similar issues arose.

What am I doing wrong, how do I create a ref and pass it to a child component in typescript React and please provide an explanation, thank you

React 19

Starting in React 19, you can now access ref as a prop for function components –

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function MyInput({placeholder, ref}) {
return <input placeholder={placeholder} ref={ref} />
}
//...
<MyInput ref={ref} />
</code>
<code>function MyInput({placeholder, ref}) { return <input placeholder={placeholder} ref={ref} /> } //... <MyInput ref={ref} /> </code>
function MyInput({placeholder, ref}) {
  return <input placeholder={placeholder} ref={ref} />
}

//...
<MyInput ref={ref} />

React 18

Supply the types to forwardRef<...>

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type IntroProps = {
children: R.ReactNode
}
const Intro = R.forwardRef<HTMLDivElement, IntroProps>((props, ref) => (
<div
ref={ref}
children={props.children}
style={{
backgroundColor: "orchid",
color: "white",
padding: "1rem",
}}
/>
))
function Foo() {
const ref = R.useRef<null | HTMLDivElement>(null)
return <Intro ref={ref} children="hello world" />
}
</code>
<code>type IntroProps = { children: R.ReactNode } const Intro = R.forwardRef<HTMLDivElement, IntroProps>((props, ref) => ( <div ref={ref} children={props.children} style={{ backgroundColor: "orchid", color: "white", padding: "1rem", }} /> )) function Foo() { const ref = R.useRef<null | HTMLDivElement>(null) return <Intro ref={ref} children="hello world" /> } </code>
type IntroProps = {
  children: R.ReactNode
}

const Intro = R.forwardRef<HTMLDivElement, IntroProps>((props, ref) => (
  <div
    ref={ref}
    children={props.children}
    style={{
      backgroundColor: "orchid",
      color: "white",
      padding: "1rem",
    }}
  />
))

function Foo() {
  const ref = R.useRef<null | HTMLDivElement>(null)
  return <Intro ref={ref}  children="hello world" />
}

demo on TypeScript playground

Using React.HTMAttributes<...> will likely be useful to you. This allows you to specify other props your component will receive –

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const Intro = R.forwardRef<HTMLDivElement, R.HTMLAttributes<HTMLDivElement>>(
(props, ref) => <div ref={ref} children={props.children} />,
)
function Foo() {
const ref = R.useRef<null | HTMLDivElement>(null)
return (
<Intro
ref={ref}
style={{
backgroundColor: "orchid",
color: "white",
padding: "1rem",
}}
onClick={event => console.log("ref", ref.current)}
children="hello world"
/>
)
}
</code>
<code>const Intro = R.forwardRef<HTMLDivElement, R.HTMLAttributes<HTMLDivElement>>( (props, ref) => <div ref={ref} children={props.children} />, ) function Foo() { const ref = R.useRef<null | HTMLDivElement>(null) return ( <Intro ref={ref} style={{ backgroundColor: "orchid", color: "white", padding: "1rem", }} onClick={event => console.log("ref", ref.current)} children="hello world" /> ) } </code>
const Intro = R.forwardRef<HTMLDivElement, R.HTMLAttributes<HTMLDivElement>>(
  (props, ref) => <div ref={ref} children={props.children} />,
)

function Foo() {
  const ref = R.useRef<null | HTMLDivElement>(null)
  return (
    <Intro
      ref={ref}
      style={{
        backgroundColor: "orchid",
        color: "white",
        padding: "1rem",
      }}
      onClick={event => console.log("ref", ref.current)}
      children="hello world"
    />
  )
}

demo on TypeScript playground

So, I went back and read the documentation on forwardRefs and in the documentation, it takes two arguments, but in my code I only gave it one. This was the reason that it was not working and throwing the error. the forwardRefs function, takes in props and the ref to be passed down to the children. I came across another post than in the case where no props is needed you can simply use _: unknown as a sort of placeholder.

New contributor

CKang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật