2

i want to toggle a material ui Dialog visibility base on URL. for example when URL is somthing like this site.com/#UKLMCBUWPO modal one be visible and when site.com/#UKLMCBUWPO&KLMCnhjmWPO modal two be visible and each time user click the browser back button last state in url pops and the second modal be hidden and so on. basically user can interact with browser back and forward buttons to show and hide the dialog. i just want some gibberish text in URL when modals open and the gibrish removed when they closed until no modals are shown on the page.

any ideas ?

1 Answer 1

3
+50

You can use HashRouter to configure the router and props.location.hash to get hash link and split it and get the last item in the array.

Working demo

Navs

     <Link
        className={
          "tab " + currentRoute.includes("about") ? "tab active" : "tab"
        }
        to="/modal#state=KLMCnhjmWPO"
      >
        Go to Modal 1
      </Link>
      <Link
        className={
          "tab " + currentRoute.includes("contact") ? "tab active" : "tab"
        }
        to={{
          pathname: "/modal",
          hash: "#state=KLMCnhjmWPO&#state=UKLMCBUWPO"
        }}
      >
        Go to Modal 2
      </Link>

Routes

<HashRouter>
        <Nav />
        <Switch>
          <Route path="/home" exact component={Home} />
          <Route path="/modal" exact component={Modal} />
        </Switch>
      </HashRouter>

Sample Modal

const Modal = props => {
  const lastModal = props.location.hash.split("&");
  return <div>Modal - {lastModal[lastModal.length - 1]}</div>;
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.