Created
December 9, 2020 19:28
-
-
Save skovy/e15e01bff27f4a530c87708e3c2f94e7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { Action, calculateActions, arrayContentsEqual } from './actions'; | |
export const useActions = (): Action[] => { | |
const [actions, setActions] = React.useState(calculateActions()); | |
React.useEffect(() => { | |
function callback() { | |
const newActions = calculateActions(); | |
if (!arrayContentsEqual(newActions, actions)) { | |
setActions(newActions); | |
} | |
} | |
const observer = new MutationObserver(callback); | |
observer.observe(document.body, { childList: true, subtree: true }); | |
return () => observer.disconnect(); | |
}); | |
return actions; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment