Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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