From e83ca583aebe6b42620c1fce8beddeaad0d9e402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Fri, 18 Aug 2023 16:40:16 +0200 Subject: [PATCH] simplify useUpdatingObservable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .../EntityPresentationApi/useUpdatingObservable.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/plugins/catalog-react/src/apis/EntityPresentationApi/useUpdatingObservable.ts b/plugins/catalog-react/src/apis/EntityPresentationApi/useUpdatingObservable.ts index 37972a5432..a15119d177 100644 --- a/plugins/catalog-react/src/apis/EntityPresentationApi/useUpdatingObservable.ts +++ b/plugins/catalog-react/src/apis/EntityPresentationApi/useUpdatingObservable.ts @@ -15,7 +15,7 @@ */ import { Observable } from '@backstage/types'; -import { DependencyList, useEffect, useRef, useState } from 'react'; +import { DependencyList, useEffect, useState } from 'react'; /** * Subscribe to an observable and return the latest value from it. @@ -36,17 +36,14 @@ export function useUpdatingObservable( observable: Observable | undefined, deps: DependencyList, ): T { - const snapshot = useRef(value); - const [, setCounter] = useState(0); + const [snapshot, setSnapshot] = useState(value); useEffect(() => { - snapshot.current = value; - setCounter(counter => counter + 1); + setSnapshot(value); const subscription = observable?.subscribe({ next: updatedValue => { - snapshot.current = updatedValue; - setCounter(counter => counter + 1); + setSnapshot(updatedValue); }, complete: () => { subscription?.unsubscribe(); @@ -59,5 +56,5 @@ export function useUpdatingObservable( // eslint-disable-next-line react-hooks/exhaustive-deps }, deps); - return snapshot.current; + return snapshot; }