Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update MyVotes (#2991, #2986) #4262

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/ui/src/common/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,22 @@ export const useLocalStorage = <T>(key?: string) => {
setState(getItem(key))
}, [key])

useEffect(() => {
const handleEventOnce = () => {
setState(getItem(key))
}

document.addEventListener(`storage_event_${key}`, handleEventOnce)
return () => {
document.removeEventListener(`storage_event_${key}`, handleEventOnce)
}
}, [])

const dispatch = useCallback(
(setStateAction: T | ((prevState?: T) => T)) => {
const value = isFunction(setStateAction) ? setStateAction(getItem(key)) : setStateAction
setState(value)
setItem(key, value)
document.dispatchEvent(new CustomEvent(`storage_event_${key}`, {}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the code it's not obvious what we are saving and why. Maybe add a comment?

},
[key]
)
Expand Down