BFE.dev
React 代码题11. useFocus()

11. useFocus()

ReactHooks
分享给更多人

  -通过 / -执行

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>}
上一个下一个

一边解释一边码字。

解答AI讨论(91)

Try it out

Console