Synchronous Modal state for react
npm install react-sync-modal-state
/*
function useSyncModalState<T = any>(): {
value: boolean;
openModal: () => Promise<T | null>;
closeModal: (returnValue: T) => void;
}
*/
import { useSyncModalState } from "react-sync-modal-state";
const App = () => {
const modalState = useSyncModalState();
return (
<>
<div>
<button
onClick={async () => {
const data = await modalState.openModal();
console.log("This was called after modal was closed", { returnData: data });
}}
>
Open Modal
</button>
{modalState.value && (
<div>
modal opened{" "}
<button
onClick={async () => {
modalState.closeModal({ test: "123" });
console.log("Modal Closed");
}}
>
close
</button>
</div>
)}
</div>
</>
);
};