有一个需要解决的路由问题,这很有趣,但我无法实现它。

huangapple go评论69阅读模式
英文:

There is a routing problem that needs to be solved, which is interesting, but I can't realize it

问题

如何实现类似 Unsplash 的路由功能?当您在主页上点击详情页面时,会弹出一个窗口,路由信息将更改为详情页面的路由信息。当关闭详情弹出窗口时,路由信息将更改为主页的路由信息。当使用详情弹出窗口时,如果应用浏览器刷新功能,将直接跳转到真正的详情页面。您能提供一个具体的实现计划吗?

我使用了 React 框架的 React-router-dom 和 window.history api,但很难找到一个解决方案。

import {
  BrowserRouter as Router,
  Route,
  Routes,
  Link,
  useNavigate,
} from "react-router-dom";
import { createContext, useContext, useState, useEffect } from "react";
const ModalContext = createContext();

const Home = () => {
  const navigate = useNavigate();
  const { setModalOpen } = useContext(ModalContext);

  const openDetailsModal = () => {
    setModalOpen(true);
    window.history.pushState("/details", "/details", "/details");
  };

  return (
    <div>
      <h1>Home Page</h1>
      <Link to="/details">Open Details Modal</Link>
      <button onClick={openDetailsModal}>
        Open Details Modal with Button
      </button>
    </div>
  );
};

const Details = () => {
  const navigate = useNavigate();
  const { setModalOpen } = useContext(ModalContext);

  useEffect(() => {
    return () => {
      setModalOpen(false);
    };
  }, [setModalOpen]);

  return (
    <div>
      <h2>Details Page</h2>
    </div>
  );
};

const App = () => {
  const [modalOpen, setModalOpen] = useState(false);

  useEffect(() => {
    const storedModalOpen =
      localStorage.getItem("modalOpen") ||
      sessionStorage.getItem("modalOpen");

    if (storedModalOpen === "true" && !modalOpen) {
      setModalOpen(true);
    }

    localStorage.removeItem("modalOpen");
    sessionStorage.removeItem("modalOpen");
  }, [modalOpen]);

  return (
    <ModalContext.Provider value={{ modalOpen, setModalOpen }}>
      <Router>
        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="/details" element={<Details />} />
        </Routes>
      </Router>
    </ModalContext.Provider>
  );
};

export default App;

请注意,这段代码实现了在主页点击详情页面时弹出模态窗口的功能,同时更新路由信息。当关闭模态窗口时,路由信息会恢复到主页。并且,如果浏览器刷新时,会直接跳转到真正的详情页面。希望这可以帮助您实现所需的路由功能。

英文:

How to realize the routing function like unsplash? When you click the details page on the home page, a pop-up window will open, and the routing information will be changed to the routing information of the details page. When the details pop-up window is closed, the routing information will be changed to the routing information of the home page. When the details pop-up window is used, if the browser refresh function is applied, the real details page will be jumped directly. Can you give a specific implementation plan.

I used the React-router-dom of the react framework and the window.history api, but it was hard to figure out a solution.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import {
BrowserRouter as Router,
Route,
Routes,
Link,
useNavigate,
} from &quot;react-router-dom&quot;;
import { createContext, useContext, useState, useEffect } from &quot;react&quot;;
const ModalContext = createContext();
const Home = () =&gt; {
const navigate = useNavigate();
const { setModalOpen } = useContext(ModalContext);
const openDetailsModal = () =&gt; {
setModalOpen(true);
window.history.pushState(&quot;/details&quot;, &quot;/details&quot;, &quot;/details&quot;);
};
return (
&lt;div&gt;
&lt;h1&gt;Home Page&lt;/h1&gt;
&lt;Link to=&quot;/details&quot;&gt;Open Details Modal&lt;/Link&gt;
&lt;button onClick={openDetailsModal}&gt;Open Details Modal with Button&lt;/button&gt;
&lt;/div&gt;
);
};
const Details = () =&gt; {
const navigate = useNavigate();
const { setModalOpen } = useContext(ModalContext);
useEffect(() =&gt; {
return () =&gt; {
setModalOpen(false);
};
}, [setModalOpen]);
return (
&lt;div&gt;
&lt;h2&gt;Details Page&lt;/h2&gt;
&lt;/div&gt;
);
};
const App = () =&gt; {
const [modalOpen, setModalOpen] = useState(false);
useEffect(() =&gt; {
const storedModalOpen =
localStorage.getItem(&quot;modalOpen&quot;) || sessionStorage.getItem(&quot;modalOpen&quot;);
if (storedModalOpen === &quot;true&quot; &amp;&amp; !modalOpen) {
setModalOpen(true);
}
localStorage.removeItem(&quot;modalOpen&quot;);
sessionStorage.removeItem(&quot;modalOpen&quot;);
}, [modalOpen]);
return (
&lt;ModalContext.Provider value={{ modalOpen, setModalOpen }}&gt;
&lt;Router&gt;
&lt;Routes&gt;
&lt;Route path=&quot;/&quot; element={&lt;Home /&gt;} /&gt;
&lt;Route path=&quot;/details&quot; element={&lt;Details /&gt;} /&gt;
&lt;/Routes&gt;
&lt;/Router&gt;
&lt;/ModalContext.Provider&gt;
);
};
export default App;

<!-- end snippet -->

答案1

得分: 0

你可以尝试类似这样的方式:

const Test = () => {
  const handleClick = () => {
    const newUrl = '/new-url';
    window.history.pushState(null, '', newUrl);
  }
  return (
    <button onClick={handleClick}>更改网址而不重新加载页面</button>
  )
}

这将更新你的URL,但不会重新加载页面!然后,你可以添加所有需要显示弹出窗口的逻辑。是否适合你的情况?
希望这有所帮助。

英文:

You could maybe try something like this:

const Test = () =&gt;{
  const handleClick = ()=&gt;{
    const newUrl = &#39;/new-url&#39;;
    window.history.pushState(null, &#39;&#39;,newUrl);
  }
  return (
    &lt;button onClick={handleClick}&gt;Change Url Not Page&lt;/button&gt;
  )
}

This will update your URL, but will not reload nor change the page! Then you can add all the logic you need to show the popup you need Does it suits your case?
Hope this helps

huangapple
  • 本文由 发表于 2023年7月10日 13:48:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定