Skip to content

Commit

Permalink
fix(sideEffects): removed query side-effects from core
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlingX committed Jul 9, 2021
1 parent d07fcbe commit a6b5207
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
14 changes: 10 additions & 4 deletions src/lib/adapters/Next.js.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -41,13 +41,14 @@ const GeschichteForNextjs: FC<Props> = ({
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) => {
Expand All @@ -66,7 +67,7 @@ const GeschichteForNextjs: FC<Props> = ({
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [defaultPushOptions, defaultReplaceOptions])
}, [])

const useStore = useMemo(() => useGeschichte(historyInstance), [
historyInstance
Expand All @@ -84,7 +85,12 @@ const GeschichteForNextjs: FC<Props> = ({

useEffect(() => {
const [, query] = split(asPath)
updateFromQuery(`?${query || ''}`)
const nextQuery = `?${query || ''}`
if (initialClientOnlyAsPath) {
// tslint:disable-next-line
lastClientSideQuery.current = nextQuery
}
updateFromQuery(nextQuery)
}, [asPath, updateFromQuery])

useEffect(() => {
Expand Down
12 changes: 3 additions & 9 deletions src/lib/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,8 @@ export const converter = <T>(historyInstance: HistoryManagement) => (
api: StoreApi<StoreState<T>>
): StoreState<T> => {
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 => {
Expand Down Expand Up @@ -353,9 +351,7 @@ export const converter = <T>(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) */
Expand Down Expand Up @@ -385,9 +381,7 @@ export const converter = <T>(historyInstance: HistoryManagement) => (
state.initialValues = initialValues
state.query = applyFlatConfigToState(
state.mappedConfig,
memoizedGetInitialQueries(
lastSearchString || historyInstance.initialSearch()
),
memoizedGetInitialQueries(historyInstance.initialSearch()),
ns,
state.values,
initialValues
Expand Down

0 comments on commit a6b5207

Please sign in to comment.