diff --git a/clients/typescript/src/frameworks/react/hooks/useLiveQuery.ts b/clients/typescript/src/frameworks/react/hooks/useLiveQuery.ts index c32613941a..9599428e07 100644 --- a/clients/typescript/src/frameworks/react/hooks/useLiveQuery.ts +++ b/clients/typescript/src/frameworks/react/hooks/useLiveQuery.ts @@ -1,5 +1,6 @@ import { useContext, + useRef, useEffect, useState, useCallback, @@ -107,19 +108,12 @@ function useLiveQueryWithDependencies( function useLiveQueryWithQueryHash( runQuery: LiveResultContext ): ResultData { - const [currentQueryHash, setQueryHash] = useState(() => - hash(runQuery.sourceQuery) + const queryHash = useMemo( + () => hash(runQuery.sourceQuery), + [runQuery.sourceQuery] ) - // Keep track of the hash of the source query to be able to rebuild everything - useEffect(() => { - const newQueryHash = hash(runQuery.sourceQuery) - if (newQueryHash !== currentQueryHash) { - setQueryHash(newQueryHash) - } - }, [runQuery.sourceQuery]) - - return useLiveQueryWithQueryUpdates(runQuery, [currentQueryHash]) + return useLiveQueryWithQueryUpdates(runQuery, [queryHash]) } function useLiveQueryWithQueryUpdates( @@ -128,9 +122,9 @@ function useLiveQueryWithQueryUpdates( ): ResultData { const electric = useContext(ElectricContext) - const [changeSubscriptionKey, setChangeSubscriptionKey] = useState() - const [tablenames, setTablenames] = useState() - const [tablenamesKey, setTablenamesKey] = useState() + const changeSubscriptionKey = useRef() + const tablenames = useRef() + const tablenamesKey = useRef() const [resultData, setResultData] = useState>({}) // The effect below is run only after the initial render @@ -142,11 +136,12 @@ function useLiveQueryWithQueryUpdates( const runInitialQuery = async () => { try { const res = await runQuery() - const tablenamesKey = JSON.stringify(res.tablenames) - if (!ignore) setTablenames(res.tablenames) - if (!ignore) setTablenamesKey(tablenamesKey) - if (!ignore) setResultData(successResult(res.result)) + if (!ignore) { + tablenamesKey.current = JSON.stringify(res.tablenames) + tablenames.current = res.tablenames + setResultData(successResult(res.result)) + } } catch (err) { if (!ignore) setResultData(errorResult(err)) } @@ -178,8 +173,8 @@ function useLiveQueryWithQueryUpdates( useEffect(() => { if ( electric === undefined || - tablenamesKey === undefined || - tablenames === undefined + tablenamesKey.current === undefined || + tablenames.current === undefined ) { return } @@ -192,23 +187,25 @@ function useLiveQueryWithQueryUpdates( // and aliases for any attached databases. const changedTablenames = notifier.alias(notification) - if (hasIntersection(tablenames, changedTablenames)) { - if (!ignore) runLiveQuery() + if (tablenames.current) { + if (hasIntersection(tablenames.current, changedTablenames)) { + if (!ignore) runLiveQuery() + } } } const key = notifier.subscribeToDataChanges(handleChange) - if (changeSubscriptionKey !== undefined) { - notifier.unsubscribeFromDataChanges(changeSubscriptionKey) + if (changeSubscriptionKey.current !== undefined) { + notifier.unsubscribeFromDataChanges(changeSubscriptionKey.current) } - setChangeSubscriptionKey(key) + changeSubscriptionKey.current = key return () => { ignore = true notifier.unsubscribeFromDataChanges(key) } - }, [electric, tablenamesKey, tablenames, runLiveQuery]) + }, [electric, tablenamesKey.current, tablenames.current, runLiveQuery]) return resultData }