admin管理员组文章数量:1424437
I'm having a ton of difficulty finding the answer. Right now all my
<Route exact path='/' render={()=><Redirect to='/dashboard'/>}/>
does is change the url to ".../dashboard", but it doesn't refresh the page so that the ponent loads, only when I hit refresh does my ponent load.
How can I get <Redirect ... />
to refresh the page after redirect?
I have had success with window.location.reload()
, but I don't know how to put it in. I've also tried adding <Redirect push to=.../>
without luck.
I'm having a ton of difficulty finding the answer. Right now all my
<Route exact path='/' render={()=><Redirect to='/dashboard'/>}/>
does is change the url to ".../dashboard", but it doesn't refresh the page so that the ponent loads, only when I hit refresh does my ponent load.
How can I get <Redirect ... />
to refresh the page after redirect?
I have had success with window.location.reload()
, but I don't know how to put it in. I've also tried adding <Redirect push to=.../>
without luck.
-
do you want to redirect always when somene lands on
/
to/dashboard
? – ztadic91 Commented Feb 11, 2018 at 23:28 -
{(auth.isAuthenticated()) ? <Redirect from='/' to='/dashboard/home' /> :<Redirect from='/' to='/login'/>}
is the code I just tried (so no), however it breaks the auth0, the /login doesn't render the login ponent if I add this redirect – Kevin Danikowski Commented Feb 12, 2018 at 0:10 -
What is your
react-router-dom
version ? – Sujith Sandeep Commented Jul 8, 2022 at 10:01
2 Answers
Reset to default 1setup a switch route and a Redirect
ponent
<Switch>
<Redirect from='/' to='/dashboard'/>
<Route path='/dashboard' render={(props) => (
auth.isAuthenticated === true
? <Component {...props} />
: <Redirect to='/login' />
)}
/>
</Switch>
More on the docs
For react-router-dom
v6. The below code is working for me,
import './App.css';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom'
import Dashboard from './Components/Dashboard';
function App() {
return (
<div className="App">
<Router>
<Routes>
<Route path="/" element={<Navigate to="/dashboard" />} />
<Route path="/dashboard" element={<Dashboard />} />
</Routes>
</Router>
</div>
);
}
export default App;
本文标签: javascriptHow to refresh ltRedirect to3939gt reactStack Overflow
版权声明:本文标题:javascript - How to refresh <Redirect to='...'> react - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745389939a2656551.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论