1

I use useRef() to deal with errors of a phone number from an input. I do it like this:

const errorPhoneDiv: MutableRefObject<{}> = useRef("");

A bit later in the code, I use errorPhoneDiv.current.innerText to set the written phone number, but TS tells me :

Property 'innerText' does not exist on type '{}'

What type should I pass to MutableRefObject to accept the useRef object?

2
  • Hi Johan! I pasted an answer; please check it and let me know! Commented Jan 2, 2023 at 13:46
  • "… to set the written phone number" - you should use react rendering for that, not a ref. Commented Jan 2, 2023 at 13:47

1 Answer 1

1

You could use HTMLInputElement if it's an input, HTMLDivElement if it's a div, etc. You also need an initial value, which could be null, in which case you need a null check before accessing innerHTML:

const errorPhoneDiv = useRef<HTMLInputElement | null>(null);
console.log(errorPhoneDiv.current?.innerHTML);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.