Skip to content

Instantly share code, notes, and snippets.

@skovy
Created December 9, 2020 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skovy/e15e01bff27f4a530c87708e3c2f94e7 to your computer and use it in GitHub Desktop.
Save skovy/e15e01bff27f4a530c87708e3c2f94e7 to your computer and use it in GitHub Desktop.
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