Created
July 28, 2020 12:45
-
-
Save andrewgreenh/c994a80c0b811e08d39b73efa1fba729 to your computer and use it in GitHub Desktop.
useLocalModal
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
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