npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-event-target

v1.2.1

Published

React hook for EventTarget, Attach event & listener without any side effect.

Downloads

20

Readme

useEventTarget

GitHub license GitHub issues GitHub stars GitHub forks Build Status

High Order function for hooks of EventTarget

Demo

讓你無憂無慮註冊事件,匿名函數也好,不想清理也罷,反正我幫你清掉。 One hook for one eventTarget's event.


使用高階函數創造毫無副作用的 useResize

// useResize.js
import useEventTarget from "use-event-target";
const useWindow = useEventTarget(window);
export default callback => {
  const [$window, resizeOff] = useWindow("resize", callback);
  return resizeOff;
};

Watch this useResize


直接在函數組件使用無副作用的事件掛載

import useEventTarget from 'use-event-target';
const useImage = useEventTarget(new Image());
const demo=()=>{
  useImage('load' , ()=>console.log('image loaded!') , /* Options */);
  return (<p>I am demo component</p>);
}

別怕!只會在 mount 那刻註冊。

API 同註冊事件(addEventListener),但是組件週期卸載後會幫你清掉(removeEventListener)。

Installation

$ npm install use-event-target

Compare

若你曾經撰寫過與原生事件相關的 React Hooks ,對下面的狀況肯定不陌生:

//useCustom
useEffect(() => {
  function cb1() {
    console.log('click');
  }
  function cb2() {
    console.log('resize');
  }
  function cb3() {
    console.log('touch');
  }
  window.addEventListener('click', cb1);
  window.addEventListener('resize', cb2);
  window.addEventListener('touch', cb3);
  return () => {
    window.removeEventListener('click', cb1);
    window.removeEventListener('resize', cb2);
    window.removeEventListener('touch', cb3);
  };
});

使用 useEventTarget

const useWindow = useEventTarget(window);
//useCustom
useWindow('click', () => console.log('click'));
useWindow('resize', () => console.log('resize'));
useWindow('touch', () => console.log('touch'));

再看一次 Demo

我做了什麼?

  1. 首先我用 curry 的方式製造了一個 Custom Hooks,讓你可以在函數組件中使用。

  2. 我用 useEffect 的 compare array 去設定,只有 mount 的時候會幫你註冊。

  3. 我偷偷把你的 callback 給 reference 了,所以可以清除掉匿名函數。

  4. 我在 cleanup 裡頭幫你清除掉,而這個 cleanup 會依照你使用這個 custom hook 的組件週期。

Advanced usage

useEventTarget 主要是丟進去 EventTarget ,並且製造出 customHooks ,這個 customHooks 將回傳一個陣列。

我們先假設 useImage 已經製造出來。

const [$img, loadOff] = useImage('load', () => console.log('load'));

$img

也就是這個 EventTarget ,有時候我們希望能改變他的屬性。

hookEvent

你可以透過 hookEvent 重置你要掛載的事件,用法:

  1. 主動清掉剛剛掛載的事件
const [$img,offEvent] = useImg('xxx',()=>{})
// In some condition
{
  offEvent(); //主動把事件清掉
}
  1. 讓事件與組件同步卸載

Example

import useEventTarget from 'use-event-target';
const useImage = useEventTarget(new Image());
const demo = () => {

  const [$img,loadOff] = useImage('load',getSize);
  function getSize (){
    loadOff();
  };
  return <button onClick={onClick}> Get Image </button>;
};

More...

useFileReader

import useEventTarget from 'use-event-target';

const demo = () => {
  const useFileReader = useEventTarget(new FileReader());
  const [$reader, offEvent] = useFileReader('loadend', () => console.log('load end'));
  const onInputChange = e => {
    const files = e.currentTarget.files;
    $reader.readAsDataURL(files[0]);
  };
  return (
    <input
      onChange={onInputChange}
      type="file"
      id="upload-file"
      placeholder="Upload a Picture"
    />
  );
};