BFE.dev
React Coding Questions15. useClickOutside()

15. useClickOutside()

ReactHooks
Share

  - accepted / - tried

Click above header menu on this page, you can see that the dropdown menu is dismissed after clicking outside.

Now you are asked to implement a React hook to make it eaiser to implement such behavior.

function Component() {  const ref = useClickOutside(() => {    alert('clicked outside')  });  return <div ref={ref}>..</div>}
prevnext

Think about the edge cases.

Request SolutionAIVideos(1)Posts(87)

Try it out

Console