useFocus()
2022年5月1日小于 1 分钟
useFocus()
Question
CSS pseudo-class :focus-within could be used to allow conditional rendering in parent element on the focus state of descendant elements.
While it is cool, in complex web apps, it might be better to control the state in script.
Now please create useFocus()
to support this.
function App() {
const [ref, isFocused] = useFocus()
return <div>
<input ref={ref}/>
{isFocused && <p>focused</p>}
</div>
}
Code
import React, { Ref } from 'react'
export function useFocus<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() {
// const [ref, isFocused] = useFocus()
// return <div>
// <input ref={ref}/>
// {isFocused && <p>focused</p>}
// </div>
// }