reactjs - 如何在功能组件内的功能上使用与 react-router 的链接相同的行为?
问题描述
我正在使用 React-router v5,我想使用与 Link 组件相同的行为
<form className="align-bottom d-flex align-baseline align-items-md-end form-inline float-right">
...
<Button type="button" className={"align-center getStarted btn mx-0 "}
onMouseDown={e => e.preventDefault()} onClick={signUpHandler}>
<HeaderLink className={'excluded'}>Get started</HeaderLink>
</Button>
...
</form>
我不想用链接包装开始按钮,因为我想检查网站是否是从小型设备访问的(然后不显示弹出窗口,但在 url 内)或桌面屏幕(然后只显示弹出窗口-向上)。
这是我到目前为止在单击“开始”按钮时尝试的登录:
const Header = () => {
...
const signUpHandler = () => {
const mql = window.matchMedia('(max-width: 900px)')
if (mql.matches) {
// The viewport is less than 670px
window.location.href = "/sign-up";
// <Redirect to='/login' />
} else {
// The viewport is wider than 670px
toggleVisibility(!isVisible);
changeIsLogIn(false)
// changeIsLogIn(isLogIn => false)
}
}
...
}
因此,window.location.href = "/sign-up";
我只想使用 react-router 的链接来重定向用户,而不是重新加载整个屏幕(在小屏幕上)。
任何帮助或建议将不胜感激,谢谢!
解决方案
推荐阅读
- pandas - 为什么 Pandas 反转我的 x 轴顺序?
- sql - Postgresql 使用通用月份参数将查询转换为视图
- javascript - 我可以使用 SQL 查询显示 JSON 数据吗?
- r - 如何将 TSQL 参数传递给 dplyr 过滤子句?
- python - Pandas 将多个组融合到单个列中
- c# - .NET Core 自定义服务器证书 CA
- javascript - Vuejs自定义插件getelementbyid不起作用
- scala - 如何在不设置的情况下将映射键转换为字符串?
- ios - FRC 内容更改时的 UITableView 异常
- jquery - jQuery Array.map 和 Object.keys() 映射对象数组的键