banner



How To Change Navbar With Login In React

How to re-render navbar, when a user login or logout or signup ?

Hello,

I want to re-render navbar whenever someone login, logout or signout. I am storing token in localstorage.

At kickoff when user login , navbar does not render. I want to show some extra information in navbar when user logins.

I am using react-redux to login and signup user and saving it in auth state inside reducer

Thanks

Please help me.

Log in or sign up to leave a comment

level 1

Heed for the API response and update land to rerender

level 1

This is a bully learning moment for you lot. I call back yous should cease "trying" things and stop and think. Why doesn't information technology re-render? Don't write a single line of lawmaking more until yous're fairly certain why.

Try and understand how React works. Go practise some reading. If you lot don't take this pace now you're but going to face up a hundred similar bugs in the hereafter

Questions:

  • What can cause a component to re-render?

  • How practise hooks actually piece of work? When exactly are they re-run?

  • How exercise your state changes flow into React? How does it know what's changed?

level 2

It does not re render, bcz at that place js not state changes in navbar. Navbar does not accept whatever state. It's dummy.

level 1

if you're using hooks, y'all tin can use useEffect to see if the token exists in LS and so render a navbar

level 2

I did that, it is e'er null. I have to refresh the page, to work it correctly.

And then this does non work.

Also I want to ask what should I put in dependency of useEffect, and so that it re-renders when user login or logout?

level 1

Maybe have two dif navs before and afterward and render on state alter based on if user is login return so and if not return and so

level 1

Why aren't you using a backend to do this? I experience like it will be much simpler

Source: https://www.reddit.com/r/reactjs/comments/odzpg8/how_to_rerender_navbar_when_a_user_login_or/

Posted by: majorreeld1968.blogspot.com

0 Response to "How To Change Navbar With Login In React"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel