Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I hide or show Navbar?

Goal: I need to when user is not null then show Navbar for this I use useEffect and useState. How did I understand when the site is loaded user is no longer null so useEffect does not work. How do I make it work?

import {BrowserRouter as Router, Routes, Route} from 'react-router-dom';
import { useState, useEffect } from 'react';
import { auth } from "./components/LoginForm/firebase.js";

function App() {
  const user = auth.currentUser

  const [Navbarshow, setNavbar] = useState(false)

  useEffect(() => {
    if (!user === null) {
      setNavbar(true)
    }
  }, [user])

  return (
    <div className="App">
      <Router>
        {Navbarshow ? <Navbar /> : null}
        <button onClick={() => console.log(Navbarshow, user)}>Test</button>

This is a part of App.js, where is problem

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

can you please try this I hope this works. thanks

useEffect(() => {
   if (!auth.currentUser) {
     setNavbar(true)
   } else {
     setNavbar(false)
   }
}, [auth.currentUser])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading