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.

Share Improve this question asked Feb 11, 2018 at 23:15 Kevin DanikowskiKevin Danikowski 5,2268 gold badges50 silver badges91 bronze badges 3
  • 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
Add a ment  | 

2 Answers 2

Reset to default 1

setup 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