Mark James
Mark James

Reputation: 568

TypeScript type for Ref as argument in React

I have a problem defining type in typescript for ref.

I have questionsRef which is a list of refs.

  const questionsRef = useRef<Array<HTMLDivElement | null>>([]);

I want to pass that questionsRef to the utility function, but Typescript requires the correct type of arguments.

How to define questiosnRef: Type here?

const focusNextQuestion = (questionsRef: any, nextQuestion: any) => {
  if (questionsRef.current) {
   // some code
};

Upvotes: 2

Views: 3345

Answers (1)

EkkoKo
EkkoKo

Reputation: 220

Try this:

React.useRef<Array<React.RefObject<HTMLDivElement | null>>>();

Upvotes: 3

Related Questions