mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
15 lines
360 B
TypeScript
15 lines
360 B
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
const useObservable = <T>(observable$, initialValue?: T): T | undefined => {
|
|
const [value, update] = useState<T | undefined>(initialValue);
|
|
|
|
useEffect(() => {
|
|
const s = observable$.subscribe(update);
|
|
return () => s.unsubscribe();
|
|
}, [observable$]);
|
|
|
|
return value;
|
|
};
|
|
|
|
export default useObservable;
|