Exciting news!Announcing our $4.6M Series Seed. Learn more
Socket
LoveBlogFAQ
Install
Log in

react-use-listener

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Issues
File Explorer

Advanced tools

react-use-listener

attach native event without and don't care about bind / unbind

    1.0.2latest

Version published
Maintainers
1
Yearly downloads
8,413

Weekly downloads

Readme

Source

useListener

attach native event without and don't care about bind / unbind

Demo CodeSandbox

Usage

  1. bind resize event
import {useState} from "react"; function App() { const [width, setWidth] = useState(0) useListener(window, "resize", () => { setWidth(window.innerWidth); }); return ( <div>Width: {width}</div> ) }
  1. cancel binding
import {useState} from "react"; function App() { const [width, setWidth] = useState(0) const listener = useListener(window, "resize", () => { setWidth(window.innerWidth); if (window.innerWidth < 1000) { listener(); } }); return ( <div>Width: {width}</div> ) }
  1. conditionally bind event
import {useState} from "react"; function App() { const [enabled, setEnabled] = useState(false); const [width, setWidth] = useState(0) useListener(window, "resize", () => { setWidth(window.innerWidth); }, { enabled }); return ( <div> <div>Width: {width}</div> <button onClick={() => setEnabled(!enabled)}>Bind resize</button> </div> ) }
  1. debounce
import {useState, useRef} from "react"; function App() { const ref = useRef(); useListener(ref, "keyup", (e) => { // set width after 300 milliseconds when stopped resizing console.log(e.target.value); }, { debounce: 300 }); return ( <div> <input ref={ref} /> </div> ) }
  1. throttle
import {useState} from "react"; function App() { const [width, setWidth] = useState(0) useListener(window, "resize", () => { // trigger after 300 milliseconds setWidth(window.innerWidth); }, { throttle: 300 }); return ( <div> <div>Width: {width}</div> </div> ) }

Reference

const listener = useListener(element, event, callback, option);
  • element : Element | Document | Window | ref element to attache event

  • event : string event name to bind

  • callback : (e) => void callback

  • option:

    • enabled : boolean weather to listen or not, default true
    • throttle : number to throttle event, default undefined
    • debounce : number debounce event, default undefined
    • capture : boolean native flag
    • passive : boolean native flag
    • once : boolean native flag

Keywords

FAQs

What is react-use-listener?

attach native event without and don&#39;t care about bind &#x2F; unbind

Is react-use-listener popular?

The npm package react-use-listener receives a total of 202 weekly downloads. As such, react-use-listener popularity was classified as not popular.

Is react-use-listener well maintained?

We found that react-use-listener demonstrated a healthy version release cadence and project activity. It has 1 open source maintainer collaborating on the project.

Last updated on 25 Jun 2021
Socket

Product

Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc