例如,我的登錄按鈕代碼中有這個。
var rJson = JSON.parse(request.responseText);
if (rJson.success) {
console.log("login success");
window.location.replace(redirectLink);
}
作業得很好,但我覺得必須有一種方法可以在 react-router-dom 中實作相同的東西,類似于如何
<Link to="/fooPage" style={{ textDecoration: "none" }}>
<Button>
Foo
</Button>
</Link>
會加載新頁面,但不會重新加載整個視窗,并且使用我的登錄按鈕之類的東西,我覺得最好通過 react-router-dom 加載頁面,而不是重新加載整個視窗。我希望這是有道理的。我在 react 檔案中找到的所有內容都與在渲染出來的組件的 HTML 部分中使用重定向有關,例如,與使用 window.location.replace() 在代碼中重定向用戶
uj5u.com熱心網友回復:
import { useNavigate } from 'react-router-dom'
const navigate = useNavigate()
navigate("/")
回家。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/455424.html
上一篇:有條件的背景圖片
