[英]How to replace previous of path react router dom
So I am a little bit confused when I try to use react-router-dom
.所以当我尝试使用react-router-dom
时我有点困惑。 How can we replace the previous path when navigating to a new path.导航到新路径时如何替换以前的路径。 Because the code is too long I will simplify it.由于代码太长,我将其简化。
For example we are in a component of http://localhost:3000/register
and when user hit a button or change progmatically using useNavigate()
to another component of http://localhost:3000/login
.例如,我们在http://localhost:3000/register
的一个组件中,当用户点击按钮或使用useNavigate()
以编程方式更改为http://localhost:3000/login
的另一个组件时。 When I code and use navigate
it will error since there is no matching route at http://localhost:3000/register/login/
.当我编码和使用navigate
时,它会出错,因为http://localhost:3000/register/login/
没有匹配的路由。
App.js应用程序.js
import './App.css';
import Login from './Pages/Login';
import Register from './Pages/Register';
function App() {
return (
<Routes>
<Route path='login' element={<Login/>} />
<Route path='register' element={<Register /> } />
</Routes>
);
}
export default App;
My code in register.js
after a click it should go to login page not add login to register path点击后我在register.js
中的代码应该 go 到登录页面而不是添加登录到注册路径
import { Link, useNavigate } from 'react-router-dom'
const Register= () => {
return (
///remove the backslash for login didnt change anything
<Link style={{textDecoration:'none'}} to="/login">
<Button
sx={{marginTop:'10px',marginBottom:'10px'}}
variant="contained"
color='primary'
>
<Typography>
Login
</Typography>
</Button>
</Link>
)
}
export default Register
How to get rid of previous path in react-router-dom
?如何摆脱react-router-dom
中的先前路径?
first path or register第一条路径或注册
got error since login path added to the register path登录路径添加到注册路径后出现错误
Did you wrap your <Route />
into a <Routes />
and a <BrowserRouter />
?您是否将<Route />
包装成<Routes />
和<BrowserRouter />
?
component Link
have property replace, which one will indicate that, need to replace url
on to
- but if you don't use baseurl
in <Routes>
)组件Link
有属性替换,这表明需要将url
替换to
- 但如果你不在<Routes>
中使用baseurl
)
Also you can see guide about Link
- https://github.com/harryheman/React-Total/blob/main/md/react-router.md#link您还可以查看有关Link
的指南 - https://github.com/harryheman/React-Total/blob/main/md/react-router.md#link
Oke after I read a lot of friends answer It actually a bug, there is a useNavigate
in my code redirect to profile
component rather being on click.好吧,在我阅读了很多朋友的回答之后,这实际上是一个错误,我的代码中有一个useNavigate
重定向到profile
组件,而不是点击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.