2024-05-27 00:12:51 +02:00
|
|
|
import { useObservable } from "./useObservable";
|
|
|
|
import { useState } from "react";
|
|
|
|
import type { StatefulObservable } from "../StatefulObservable";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Equivalent of https://docs.evt.land/api/react-hooks
|
|
|
|
* */
|
2024-06-05 06:10:11 +02:00
|
|
|
export function useRerenderOnChange(obs: StatefulObservable<unknown>): void {
|
2024-05-27 00:12:51 +02:00
|
|
|
//NOTE: We use function in case the state is a function
|
2024-06-05 06:10:11 +02:00
|
|
|
const [, setCurrent] = useState(() => obs.current);
|
2024-05-27 00:12:51 +02:00
|
|
|
|
|
|
|
useObservable(
|
|
|
|
({ registerSubscription }) => {
|
2024-06-05 06:10:11 +02:00
|
|
|
const subscription = obs.subscribe(current => setCurrent(() => current));
|
2024-05-27 00:12:51 +02:00
|
|
|
registerSubscription(subscription);
|
|
|
|
},
|
2024-06-05 06:10:11 +02:00
|
|
|
[obs]
|
2024-05-27 00:12:51 +02:00
|
|
|
);
|
|
|
|
}
|