The useFocus Hook

In order to know whether an element is currently focused, we can use the useFocus Hook as follows:

  1. First, we import the useFocus Hook:
import React from 'react'
import { useFocus } from 'react-hookedup'
  1. Then, we define our component and the Focus Hook, which returns the focused value and a bind function, to bind the Hook to an element:
export default function UseFocus () {
const { focused, bind } = useFocus()
  1. Finally, we render an input field, and bind the Focus Hook to it:
    return (
<div>
<input {...bind} value={focused ? 'focused' : 'not focused'} />
</div>
)
}

As we can see, the Focus Hook makes it much easier to handle focus events. There is no need to define our own handler functions anymore.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.38.117