Skip to content

Instantly share code, notes, and snippets.

@andrewgreenh
Created July 28, 2020 12:45
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 andrewgreenh/c994a80c0b811e08d39b73efa1fba729 to your computer and use it in GitHub Desktop.
Save andrewgreenh/c994a80c0b811e08d39b73efa1fba729 to your computer and use it in GitHub Desktop.
useLocalModal
function useLocalModal() {
const [modalNode, setModalNode] = useState<ReactNode>(null);
const openModal = useCallback(function openModal<ResultType>(
renderModal: (close: (result: ResultType) => void) => ReactNode
): Promise<ResultType> {
return new Promise(resolve => {
function close(result: ResultType) {
setModalNode(null);
resolve(result);
}
const modalNode = renderModal(close);
setModalNode(modalNode);
});
},
[]);
const memoizedTuple = useMemo(() => [modalNode, openModal] as const, [
modalNode,
openModal
]);
return memoizedTuple;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment