U
U
uRoot2021-04-23 18:05:40
typescript
uRoot, 2021-04-23 18:05:40

What data type to register for the onFocus and onBlur events?

There is a code:

function onFocus(event: FocusEvent) {
    const currentTarget = event.currentTarget as HTMLElement
    currentTarget.classList.add("ticket--focus")
  }

  const onBlur = (event: FocusEvent) => {
    const currentTarget = event.currentTarget as HTMLElement
    currentTarget.classList.remove("ticket--focus")
  }
...
<div
  className="ticket__new-title"
  onFocus={onFocus}
  onBlur={onBlur}
 >


TS swears at this: onFocus={onFocus}, onBlur={onBlur}. What data type to prescribe to get rid of the error?

6082e21319f1d384797842.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
uRoot, 2021-04-23
@uroot

You need to do this:

const onFocus = (event: React.FocusEvent<HTMLDivElement>) => {
    event.currentTarget.classList.add("ticket--focus")
  }

  const onBlur = (event: React.FocusEvent<HTMLDivElement>) => {
    event.currentTarget.classList.remove("ticket--focus")
  }

P
Pavel Shvedov, 2021-04-23
@mmmaaak

He explicitly says what type should be

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question