Remove dependency to evt in the component library

This commit is contained in:
Joseph Garrone
2024-05-27 00:12:51 +02:00
parent a3270d10f0
commit 338642094d
8 changed files with 132 additions and 8 deletions

View File

@ -0,0 +1,19 @@
import { useObservable } from "./useObservable";
import { useState } from "react";
import type { StatefulObservable } from "../StatefulObservable";
/**
* Equivalent of https://docs.evt.land/api/react-hooks
* */
export function useRerenderOnChange($: StatefulObservable<unknown>): void {
//NOTE: We use function in case the state is a function
const [, setCurrent] = useState(() => $.current);
useObservable(
({ registerSubscription }) => {
const subscription = $.subscribe(current => setCurrent(() => current));
registerSubscription(subscription);
},
[$]
);
}