feat: SW-693 Moved borwser storage code in useEffect of component
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
FocusEvent,
|
||||
FormEvent,
|
||||
useCallback,
|
||||
useEffect,
|
||||
useReducer,
|
||||
} from "react"
|
||||
import { useFormContext, useWatch } from "react-hook-form"
|
||||
@@ -28,10 +29,9 @@ export default function Search({ locations }: SearchProps) {
|
||||
const { register, setValue, trigger } = useFormContext<BookingWidgetSchema>()
|
||||
const intl = useIntl()
|
||||
const value = useWatch({ name })
|
||||
const searchData = useWatch({ name: "location" })
|
||||
const [state, dispatch] = useReducer(
|
||||
reducer,
|
||||
{ defaultLocations: locations, searchData },
|
||||
{ defaultLocations: locations },
|
||||
init
|
||||
)
|
||||
|
||||
@@ -114,6 +114,17 @@ export default function Search({ locations }: SearchProps) {
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
state.searchData =
|
||||
typeof window !== "undefined"
|
||||
? JSON.parse(sessionStorage.getItem(sessionStorageKey) || "")
|
||||
: undefined
|
||||
state.searchHistory =
|
||||
typeof window !== "undefined"
|
||||
? JSON.parse(localStorage.getItem(localStorageKey) || "")
|
||||
: null
|
||||
}, [state])
|
||||
|
||||
return (
|
||||
<Downshift
|
||||
initialSelectedItem={state.searchData}
|
||||
|
||||
Reference in New Issue
Block a user