-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KL-176/refactor: separate location state initialization into custom hook
- Loading branch information
Showing
2 changed files
with
28 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useEffect } from 'react' | ||
import { useLocation } from 'react-router-dom' | ||
import useFeedStore from '@stores/useFeedStore' | ||
|
||
const useInitializeLocationState = () => { | ||
const location = useLocation() | ||
const resetSelectedField = useFeedStore((state) => state.resetSelectedField) | ||
|
||
useEffect(() => { | ||
const deleteDataState = (state) => { | ||
if (!state?.usr) return state | ||
const newState = { ...state, usr: { ...state.usr } } | ||
if ('data' in newState.usr) delete newState.usr.data | ||
return newState | ||
} | ||
const { history } = window | ||
if (history.state) { | ||
const newState = deleteDataState(history.state) | ||
if (newState) history.replaceState(newState, '') | ||
} | ||
return resetSelectedField | ||
}, [location.state]) | ||
} | ||
|
||
export default useInitializeLocationState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters