useDocumentEventListener

About

A react hook to an event listener to the document object

Examples

import { useDocumentEventListener } from "rooks";
import { useState, useEffect } from "react";

export default function App() {
  const [myState, setMyState] = useState(0);

  useDocumentEventListener("click", function() {
    setMyState(myState + 1);
  });

  return (
    <div className="App">
      <h1>useDocumentEventListener Example</h1>
      <br></br>
      <h1>Clicked {myState} times</h1>
    </div>
  );
}

Arguments

ArgumentsTypeDescriptionDefault value
eventNamestringThe event to trackundefind
callbackfunctionThe callback to be called on eventundefined
conditionsobjectThe options to be passed to the event listener
isLayoutEffectbooleanShould it use layout effect. Defaults to falsefalse

Return

No return value.

Join the community!

Join our discord server! You can click the floating discord icon at the bottom right of the screen and chat with us!

Powered by vercel