英文:
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 "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;
<!-- 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 = () =>{
const handleClick = ()=>{
const newUrl = '/new-url';
window.history.pushState(null, '',newUrl);
}
return (
<button onClick={handleClick}>Change Url Not Page</button>
)
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论