Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

407
Views
Hide navigation Bar via Role in React js

How can I hide the navigation bar between roles ('Admin' & 'user') in React js
Example

  const [currentUser, setCurrentUser] = useState("");
  const [role, setRole] = useState("");
  let navigation = [];

  useEffect(async () => {
    // get user from Local storage 
    const user = authService.getCurrentUser();
    if (!user) return;
    setCurrentUser(user);
    // get Role from Database 
    currentUser && setRole(authService.getRoleByUserId(currentUser.id));

    console.log(currentUser);
  }, [currentUser.id]);

  if (currentUser && role === "Admin") {
    navigation = [
      { name: "Home", href: "/Home", action: false, icon: false },
      { name: "Vendors", href: "/vendors", action: false, icon: false },
      { name: "Pricing", href: "/pricing", action: false, icon: false },
      { name: "Profile", href: "/profile", action: false, icon: false },
      { name: "Sign Out", href: "/", action: false, icon: false },
      { name: "", href: "/Cart", action: false, icon: true },
    ];
  } else {
    navigation = [
      { name: "Home", href: "/Home", action: false, icon: false },
      { name: "Pricing", href: "/pricing", action: false, icon: false },
      { name: "Sign In", href: "/login", action: false, icon: false },
      { name: "", href: "/Cart", action: false, icon: true },
    ];
  }

  return (
   ...

      

How can i do something this? Suggestions are always appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You shouldn't go with currentUser in your useEffect but with user.

Try:

// get user from Local storage
const user = authService.getCurrentUser();
if (!user) return;
setCurrentUser(user);
// get Role from Database
user && setRole(authService.getRoleByUserId(currentUser.id));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!