From a6b5207176dae1e64075580df9fe7a53de4ef48c Mon Sep 17 00:00:00 2001 From: BowlingX Date: Fri, 9 Jul 2021 18:28:43 +0200 Subject: [PATCH] fix(sideEffects): removed query side-effects from core --- src/lib/adapters/Next.js.tsx | 14 ++++++++++---- src/lib/middleware.ts | 12 +++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/lib/adapters/Next.js.tsx b/src/lib/adapters/Next.js.tsx index 1eb0740..b50a645 100644 --- a/src/lib/adapters/Next.js.tsx +++ b/src/lib/adapters/Next.js.tsx @@ -1,5 +1,5 @@ /* tslint:disable:no-expression-statement readonly-array */ -import React, { FC, useEffect, useMemo } from 'react' +import React, { FC, useEffect, useMemo, useRef } from 'react' // tslint:disable-next-line:no-submodule-imports import shallow from 'zustand/shallow' import { StoreState } from '../middleware' @@ -41,13 +41,14 @@ const GeschichteForNextjs: FC = ({ defaultPushOptions, defaultReplaceOptions }) => { + const lastClientSideQuery = useRef(initialClientOnlyAsPath) const historyInstance: HistoryManagement = useMemo(() => { return { initialSearch: () => { const [, query] = typeof window === 'undefined' ? split(asPath) - : split(initialClientOnlyAsPath || Router.asPath) + : split(lastClientSideQuery.current || Router.asPath) return `?${query || ''}` }, push: (next: string, options) => { @@ -66,7 +67,7 @@ const GeschichteForNextjs: FC = ({ } } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [defaultPushOptions, defaultReplaceOptions]) + }, []) const useStore = useMemo(() => useGeschichte(historyInstance), [ historyInstance @@ -84,7 +85,12 @@ const GeschichteForNextjs: FC = ({ useEffect(() => { const [, query] = split(asPath) - updateFromQuery(`?${query || ''}`) + const nextQuery = `?${query || ''}` + if (initialClientOnlyAsPath) { + // tslint:disable-next-line + lastClientSideQuery.current = nextQuery + } + updateFromQuery(nextQuery) }, [asPath, updateFromQuery]) useEffect(() => { diff --git a/src/lib/middleware.ts b/src/lib/middleware.ts index 862a760..1d35f0a 100644 --- a/src/lib/middleware.ts +++ b/src/lib/middleware.ts @@ -283,10 +283,8 @@ export const converter = (historyInstance: HistoryManagement) => ( api: StoreApi> ): StoreState => { const memoizedGetInitialQueries = memoizeOne(parseSearchString) - // tslint:disable-next-line:no-let - let lastSearchString: string + const updateFromQuery = (search: string) => { - lastSearchString = search const nextQueries = memoizedGetInitialQueries(search) const namespaces = get().namespaces Object.keys(namespaces).forEach(ns => { @@ -353,9 +351,7 @@ export const converter = (historyInstance: HistoryManagement) => ( }, /** the initial queries when the script got executed first (usually on page load). */ initialQueries: () => - memoizedGetInitialQueries( - lastSearchString || historyInstance.initialSearch() - ), + memoizedGetInitialQueries(historyInstance.initialSearch()), /** here we store all data and configurations for the different namespaces */ namespaces: {}, /** pushes a new state for a given namespace, (will use history.pushState) */ @@ -385,9 +381,7 @@ export const converter = (historyInstance: HistoryManagement) => ( state.initialValues = initialValues state.query = applyFlatConfigToState( state.mappedConfig, - memoizedGetInitialQueries( - lastSearchString || historyInstance.initialSearch() - ), + memoizedGetInitialQueries(historyInstance.initialSearch()), ns, state.values, initialValues