Skip to content

chroma-x/react-custom-events

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-custom-events

Why

React way of handling events is to pass callbacks to child components. This becomes cumbersome when child is several levels deep.

One way to avoid passing callbacks deep is using Context

But context is not meant for all use cases and not so intuitive.

I prefer the web components way, as they are close to native html tags and most common way of handling events in them is CustomEvent.

This package is a react layer above CustomEvent

How to use

Installation
npm i react-custom-events
Emit Event

The emitting element could be a RefObject<HTMLElement>, MutableRefObject<HTMLElement> or a HTMLElement.

import { useRef } from 'react';
import { emitCustomEvent } from 'react-custom-events';

const emittingElement = useRef(null);

const handleClick = (): void => {
	emitCustomEvent(emittingElement, 'my-event');
};

return (
	<button ref={emittingElement} onClick={handleClick}>Label</button>
);

Attach data to event

emitCustomEvent<boolean>(emittingElement, 'my-event', false);

Custom event options

emitCustomEvent<boolean>(emittingElement, 'my-event', false, {
	bubbles: true,
	cancelable: true,
	composed: true
});
Listen Event

The listener element could be a RefObject<HTMLElement>, MutableRefObject<HTMLElement> or a HTMLElement.

The provided callback is called with a type native CustomEvent of which you can use the common features like preventing the default behavior and so on. The payload is part of the event as the member detail.

import { useRef } from 'react';
import { useCustomEventListener } from 'react-custom-events';

const listenerElement = useRef(null);

useCustomEventListener<boolean>(listenerElement, 'my-event', (event): void => {
	event.stopPropagation();
	console.debug(event.detail);
});

return (
	<div ref ={listenerElement} />
);

useCustomEventListener is a custom hook, so can be used in function component only (see notes below)

No need to remove event listener, it uses react's useEffect hook to remove listener on component unmount

Notes
  • Will support class components in future, for now addEventListener can be used

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 83.8%
  • JavaScript 16.2%