window.location.replace() 的 React 版本?
React version of window.location.replace()?
例如,我在登录按钮的代码中有这个。
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>
会加载新页面,但不会重新加载整个 window,并且使用我的登录按钮之类的东西,我觉得最好通过 react-router 加载页面- dom,与重新加载整个 window。
我希望这是有道理的。
我在 React 文档中发现的所有内容都更多地与在呈现出来的组件的 HTML 部分中使用重定向有关,例如,与使用 window.location.replace()[=13 在代码中重定向用户相比=]
来自 React Router V6...
import { useNavigate } from 'react-router-dom';
const navigate = useNavigate();
navigate("/");
回家。
在 v6 之前,您使用历史对象...
history.push("/");
通过从道具或 useHistory 钩子中检索它。
例如,我在登录按钮的代码中有这个。
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>
会加载新页面,但不会重新加载整个 window,并且使用我的登录按钮之类的东西,我觉得最好通过 react-router 加载页面- dom,与重新加载整个 window。 我希望这是有道理的。 我在 React 文档中发现的所有内容都更多地与在呈现出来的组件的 HTML 部分中使用重定向有关,例如,与使用 window.location.replace()[=13 在代码中重定向用户相比=]
来自 React Router V6...
import { useNavigate } from 'react-router-dom';
const navigate = useNavigate();
navigate("/");
回家。
在 v6 之前,您使用历史对象...
history.push("/");
通过从道具或 useHistory 钩子中检索它。