1

Am trying to Redirect router to "/", without login it set to Landing.jsx, After login needs to redirect to Homepage.jsx which is also in same route "/". tried useNavigate, Its redirecting to "/" but it was set to Landing.jsx, only after refresh it sets the Homepage.jsx. Route method I used is mentioned below. Please help. The Validate is settedup when logged in was successfull.

const validate = localStorage.getItem('userLoggedInDetails');

<Router>
  <Header />
  <Routes>
    <Route path="/" element={validate ? <Homepage /> : <Landing />}></Route>
    <Route path="/login" element={<Login />}></Route>
    <Route path="/signup" element={<Signup />}></Route>
  </Routes>
</Router>
RANTO BERK
  • 31
  • 1
  • 6
  • Please try to include a [minimal, complete, and reproducible code example](https://stackoverflow.com/help/minimal-reproducible-example) that includes all the relevant code you are working with. Is something supposed to update the `validate` value to switch from rendering the `Landing` component to the `Homepage` component, or vice versa? – Drew Reese May 13 '22 at 05:56
  • And where and how are users logging in and getting redirected back to `"/"`? Does this answer your question? https://stackoverflow.com/a/66289280/8690857 – Drew Reese May 13 '22 at 06:14

1 Answers1

1

I assume you are using react.

React does not "react" to changes in localStorage. You need to use hooks like useState.

This was an easy enough problem to recreate and so I have created a CodeSandbox - https://codesandbox.io/s/sad-bohr-3eqy2o

I would recommend reading the documentation of react to avoid these kinds of issues.

bytec0de
  • 481
  • 4
  • 3