简体   繁体   中英

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

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM