BFE.dev
React Coding Questions11. useFocus()

11. useFocus()

ReactHooks
Share

  - accepted / - tried

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>}
prevnext

Always try to find a better approach.

SolutionAIPosts(91)

Try it out

Console