mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
* build: enable consistent type imports eslint rule * change to warn * fix the warning in example and excalidraw-app * fix packages * enable type annotations and throw error for the rule
21 lines
447 B
TypeScript
21 lines
447 B
TypeScript
import { useEffect, useState } from "react";
|
|
import type { Emitter } from "../emitter";
|
|
|
|
export const useEmitter = <TEvent extends unknown>(
|
|
emitter: Emitter<[TEvent]>,
|
|
initialState: TEvent,
|
|
) => {
|
|
const [event, setEvent] = useState<TEvent>(initialState);
|
|
|
|
useEffect(() => {
|
|
const unsubscribe = emitter.on((event) => {
|
|
setEvent(event);
|
|
});
|
|
|
|
return () => {
|
|
unsubscribe();
|
|
};
|
|
}, [emitter]);
|
|
|
|
return event;
|
|
};
|