Skip to main content

useEventEmitter

A basic eventemitter

Usage

Live Editor

function Demo() {
  const [state, setState] = useState(0);
  const [event, fire, dispose] = useEventEmitter<number>();

  const event1 = useRef<IDisposable>();
  useEffect(() => {
    event((val) => {
      setState(s => s + val);
    });
    event1.current = event(val => setState(s => s + val + 10));
  }, [event]);

  return (
    <div>
      <div>state: {state}</div>
      <button onClick={() => fire(1)}>fire</button>
      <button onClick={() => dispose()}>disposeALL</button>
      <button onClick={() => event1.current?.dispose()}>disposeOne</button>
    </div>
  );
};

render(<Demo/>)
Result
Loading...

API

useEventEmitter

Returns

readonly [UseEventEmitterEvent<T, U>, (arg1: T, arg2: U) => void, () => void]: A tuple with the following elements:

  • a function to add lisenter.
  • fire functiion. stop functiion

Arguments