eBay / nice-modal-react

A modal state manager for React.
https://ebay.github.io/nice-modal-react
MIT License
1.96k stars 110 forks source link

Keep track of modals based on url? #6

Closed dragonfire1119 closed 2 years ago

dragonfire1119 commented 2 years ago

I know this is out of scope but does anybody have ideas on opening nice modals based on the URL?

Example: /posts/create it opens the create post modal but if the user refreshes the page it opens the modal based on the URL.

This lib is awesome!

supnate commented 2 years ago

Yes, you can persist modal state in the URL, sample code like below:

const loc = useLocation();
const modal = useModal(PostModal);
useEffect(() => {
  if (loc.pathname.startsWith('/posts/create') modal.show();
  else modal.hide();
}, [loc, modal]);