Home > Blockchain >  TypeScript type for Ref as argument in React
TypeScript type for Ref as argument in React

Time:10-07

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
};

CodePudding user response:

Try this:

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