All Projects → donavon → Use Event Listener

donavon / Use Event Listener

Licence: mit
A custom React Hook that provides a declarative useEventListener

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Use Event Listener

Ant-Design-Pro-V5
Ant Design Pro V5 详细配置,包括分模块打包,ahooks的使用,L7 地图组件的封装,合理的初始化数据,更有动态表单、动态表格、OSS图片上传等优秀组件(项目会逐渐迭代)~
Stars: ✭ 28 (-89.43%)
Mutual labels:  hooks
use-tiny-state-machine
A tiny (~700 bytes) react hook to help you write finite state machines
Stars: ✭ 37 (-86.04%)
Mutual labels:  hooks
MouseInjectDetection
Simple method of checking whether or not mouse movement or buttons (<windows 10) are injected
Stars: ✭ 29 (-89.06%)
Mutual labels:  hooks
RT7-example
Code for the React Table 7 article
Stars: ✭ 32 (-87.92%)
Mutual labels:  hooks
revery-graphql-hooks
A library for easy handling of GraphQL with hooks for Revery
Stars: ✭ 34 (-87.17%)
Mutual labels:  hooks
Twenty48
A modified clone of the puzzle game 2048, built in react/typescript!
Stars: ✭ 31 (-88.3%)
Mutual labels:  hooks
gatsby-react-hooks
An example of using Gatsby with React hooks
Stars: ✭ 26 (-90.19%)
Mutual labels:  hooks
Usehooks
Easy to understand React Hook code recipes
Stars: ✭ 3,075 (+1060.38%)
Mutual labels:  hooks
hookr
PHP action and filter hook system
Stars: ✭ 39 (-85.28%)
Mutual labels:  hooks
meteor-method-hooks
atmospherejs.com/seba/method-hooks
Stars: ✭ 24 (-90.94%)
Mutual labels:  hooks
document-title
React hook for updating the document-title
Stars: ✭ 60 (-77.36%)
Mutual labels:  hooks
Oxide.Patcher
IL patcher for use with adding Oxide support to .NET games
Stars: ✭ 27 (-89.81%)
Mutual labels:  hooks
react-movies-finder
React Movies finder is a React app to search movies and series using redux, redux-thunk, React Hooks, and Material UI
Stars: ✭ 27 (-89.81%)
Mutual labels:  hooks
React-Combine-Provider
combine react providers in ease
Stars: ✭ 29 (-89.06%)
Mutual labels:  hooks
mantine
React components library with native dark theme support
Stars: ✭ 4,390 (+1556.6%)
Mutual labels:  hooks
use-mutation
🧬 Run side-effects safely in React
Stars: ✭ 81 (-69.43%)
Mutual labels:  hooks
graphire
An unopinionated react graph visualization library.
Stars: ✭ 256 (-3.4%)
Mutual labels:  hooks
Pre Commit Golang
Golang hooks for pre-commit
Stars: ✭ 261 (-1.51%)
Mutual labels:  hooks
Ddetours
Delphi Detours Library
Stars: ✭ 256 (-3.4%)
Mutual labels:  hooks
wp-documentor
Documentation Generator for WordPress.
Stars: ✭ 28 (-89.43%)
Mutual labels:  hooks

@use-it/event-listener

A custom React Hook that provides a declarative useEventListener.

npm version All Contributors

This hook was inspired by Dan Abramov's blog post "Making setInterval Declarative with React Hooks".

I needed a way to simplify the plumbing around adding and removing an event listener in a custom hook. That lead to a chain of tweets between Dan and myself.

Installation

$ npm i @use-it/event-listener

or

$ yarn add @use-it/event-listener

Usage

Here is a basic setup.

useEventListener(eventName, handler, element, options);

Parameters

Here are the parameters that you can use. (* = optional)

Parameter Description
eventName The event name (string). Here is a list of common events.
handler A function that will be called whenever eventName fires on element.
element* An optional element to listen on. Defaults to global (i.e., window).
options* An object { capture?: boolean, passive?: boolean, once?: boolean } to be passed to addEventListener. For advanced use cases. See MDN for details.

Return

This hook returns nothing.

Example

Let's look at some sample code. Suppose you would like to track the mouse position. You could subscribe to mouse move events with like this.

const useMouseMove = () => {
  const [coords, setCoords] = useState([0, 0]);

  useEffect(() => {
    const handler = ({ clientX, clientY }) => {
      setCoords([clientX, clientY]);
    };
    window.addEventListener('mousemove', handler);
    return () => {
      window.removeEventListener('mousemove', handler);
    };
  }, []);

  return coords;
};

Here we're using useEffect to roll our own handler add/remove event listener.

useEventListener abstracts this away. You only need to care about the event name and the handler function.

const useMouseMove = () => {
  const [coords, setCoords] = useState([0, 0]);

  useEventListener('mousemove', ({ clientX, clientY }) => {
    setCoords([clientX, clientY]);
  });

  return coords;
};

Live demo

You can view/edit the sample code above on CodeSandbox.

Edit demo app on CodeSandbox

License

MIT Licensed

Contributors

Thanks goes to these wonderful people (emoji key):


Donavon West

🚇 ⚠️ 💡 🤔 🚧 👀 🔧 💻

Kevin Kipp

💻

Justin Hall

💻 📖

Jeow Li Huan

👀

Norman Rzepka

🤔

Beer van der Drift

⚠️ 💻

clingsoft

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].