1

My sidebar seems to appear everywhere on my components. I want it to only appear after login is successfull.

I have thought of using other methods but all to no avail.

App.js

const  App = () => {

  const onSearch = (value) => console.log(value);
 
  // const {currentUser} = useContext(AuthContext)

  const currentUser = false
  const RequireAuth = ({children}) => {
    return currentUser ? children : <Navigate to="/login" /> ;
  };
  console.log(currentUser)


  

  return (

    <AuthContextProvider>
     
   
    <div>

   
            <Sidebar/>

  

      
  <div>
     
        
       <Routes>
       <Route  path="/candidates" element={<RequireAuth> <Candidates /> </RequireAuth>} />
       <Route  path="/inbox" element={<RequireAuth><MainLayout /></RequireAuth>} />
       <Route  path="/profile" element={<RequireAuth> <Profile /></RequireAuth>  } />
       <Route  path="/soon" element={ <RequireAuth> <ComingSoon /></RequireAuth>  } />
       <Route  path="/login" element={ <Login />} />
       </Routes>
       
  
    
       
     
      </div> 
      
        
          
        
        



</div> 
          
          </AuthContextProvider>
         
    
    
  )
}

export default App;

My sidebar seen above the login page . I want it to only appear after login is successfull.

I have thought of using other methods but all to no avail.

OutPut

This is what i get

  • Additionally you'll want to move the `RequireAuth` component declaration outside of `App` so it's not redeclared each render cycle and remounts its entire sub-ReactTree. See this [answer](/a/66289280/8690857) for a more appropriate protected route implementation. – Drew Reese Jul 20 '22 at 01:38

0 Answers0