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

289
Views
Dynamic welcome <user> of navbar in reactJs

I have a doubt regarding dynamically rendering the logged in user's name in the NavBar in react.

So my app is structured in the following way

<App>
<Header/>
<Router>
....
<Router/>
<Footer/>
<App/>

Now inside the header component, I need to display "Welcome <user>" whenever the user is logged in. Here are some solutions that I thought of,but Not sure if I'm missing out on anything.

  1. To send it as a prop to the header. But it can only be dynamic if I do a setState in the App component and re render the entire app component (Which I want to avoid)
  2. To store it in redux, so header can dynamically get the updated. But since I am using JWT, and the user needs to stay logged in, If the user refreshes the page, store content will be wiped out.
  3. I can store the username in localstorage as well, but the entire app would still need to be re rendered in order to display the change

-) I think it can be accomplished in Angular by BehaviorSubject, but in this scenario, I'm clueless.

Is there any solution or method that I am missing? Thanks in advance

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

0

You can send the user as a prop to the header. That is kind of the preferred method. Have the user login and save their app state with redux of the useState hook that is now available in React.

To your second question, JWTs preserve session data. Rerendering the page won't cause that to disappear. Well-designed apps have JWT with an expiration time (usually 20 minutes or less).

I worked on a voting application that did exactly what you're looking for and did not require any global state management. Feel free to pick through it. It's an open-source project. I think we used Auth0 or Google's Oauth library to do authentication.

Here's the code for the React header(navbar): https://github.com/poll-pal/poll-pal/blob/master/src/Components/Header/Header.js

Here's the code for the Express server that backed it: https://github.com/poll-pal/poll-pal/blob/master/server/routes/auth.js

about 4 years ago · Juan Pablo Isaza Report

0

At first if you want to keep your user logged in then you have to store the JWT somewhere. Here by 'somewhere' I mean, so that you can access it on next reload.

In my maximum project I store my JWT in local-storage. It works something like this

  • When user logs in I store the JWT in local-storage also in my reducer.
  • Then on my 'Inital State' I always take the local-storage so that if we have anything in the local-storage it means user is logged in
initial_state = {
  userName: localstorage.getItem('user_name')
}

That's how you can keep your user logged in and at the time of logout erase the local-storage as well as the reducer.

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!