useHover()
2022年5月1日小于 1 分钟
useHover()
Question
It is common to see conditional rendering based on hover state of some element.
We can achive it by CSS pseduo class :hover
, but for more complex cases it might be better to have state controlled by script.
Now you are asked to create a useHover()
hook.
function App() {
const [ref, isHovered] = useHover()
return <div ref={ref}>{isHovered ? 'hovered' : 'not hovered'}</div>
}
Code
import { Ref } from 'react'
export function useHover<T extends HTMLElement>(): [Ref<T>, boolean] {
// your code here
}
// if you want to try your code on the right panel
// remember to export App() component like below
// export function App() {
// return <div>your app</div>
// }