创建 onClick 函数,无需打开新的 window 即可将您发送到 link
Make onClick function that sends you to a link without opening a new window
我正在使用 Material UI 并且像这样将主页图标导入我的导航栏
<Home edge="start" color="inherit" aria-label="home"
onClick={event => window.location.href='/
<Home fontSize="large"</Home>
是否有一些特定代码代替 window.location.href
使其滚动到该部分而无需再次打开 window?
我认为使用 react-router-dom 是您的最佳选择。
我创建了一个代码沙箱来向您展示如何正确使用 react-router-doms useHistory 挂钩。请参考 App.js 以了解如何正确定义路由。请参考主页以参考正确使用 react-router-dom
提供的 useHistory() 挂钩
https://codesandbox.io/s/material-ui-playground-forked-u58hr?file=/Home.js
我正在使用 Material UI 并且像这样将主页图标导入我的导航栏
<Home edge="start" color="inherit" aria-label="home"
onClick={event => window.location.href='/
<Home fontSize="large"</Home>
是否有一些特定代码代替 window.location.href
使其滚动到该部分而无需再次打开 window?
我认为使用 react-router-dom 是您的最佳选择。
我创建了一个代码沙箱来向您展示如何正确使用 react-router-doms useHistory 挂钩。请参考 App.js 以了解如何正确定义路由。请参考主页以参考正确使用 react-router-dom
提供的 useHistory() 挂钩https://codesandbox.io/s/material-ui-playground-forked-u58hr?file=/Home.js