Skip to content

Commit

Permalink
Avoid conditional in useEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
WardBrian committed Jun 27, 2024
1 parent 62a74e9 commit 26ee51e
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions gui/src/app/SPAnalysis/SPAnalysisContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const SPAnalysisContextProvider: FunctionComponent<PropsWithChildren<SPAnalysisC

const [searchParams, setSearchParams] = useSearchParams();


useEffect(() => {
// as user reloads the page or closes the tab, save state to local storage
const handleBeforeUnload = () => {
Expand All @@ -39,8 +38,6 @@ const SPAnalysisContextProvider: FunctionComponent<PropsWithChildren<SPAnalysisC
}, [data])

useEffect(() => {
if (searchParams.size === 0 && data !== initialDataModel) return;

const queries = fromQueryParams(searchParams)
if (queryStringHasParameters(queries)) {
fetchRemoteAnalysis(queries).then((data) => {
Expand All @@ -58,8 +55,10 @@ const SPAnalysisContextProvider: FunctionComponent<PropsWithChildren<SPAnalysisC
const parsedData = deserializeAnalysisFromLocalStorage(savedState)
update({ type: 'loadInitialData', state: parsedData })
}

}, [data, searchParams, setSearchParams])
// once we have loaded some data, we don't need the localStorage again
// and it will be overwritten by the above event listener on close
localStorage.removeItem('stan-playground-saved-state')
}, [searchParams, setSearchParams])

return (
<SPAnalysisContext.Provider value={{ data, update }}>
Expand Down

0 comments on commit 26ee51e

Please sign in to comment.