0

when a User SignUP, in the Header I want to display his name, But it shows null, When I refresh the page it shows the correct name. why this is not auto rendering on changing username.

homepage.js

const [username, setusername] = useState('loading..')
 
    useEffect(() => {
       setusername(auth.currentUser.displayName);
    }, [[auth.currentUser.displayName]])
     // Some more work here
     return(
// display Name
)


       

1 Answers1

0

First, you tell it to only run useEffect once when you pass in an empty array, second, you can't assign to username directly, you need to use the setusername function. You probably just need to do

useEffect(() => {
   setusername(auth.currentUser.displayName);
}, [auth.currentUser.displayName])
dave
  • 62,300
  • 5
  • 72
  • 93
  • I missed typed that direct assignment of username. but even this isn't working. I'm facing a problem when user Signed In – Narendra Dewasi Oct 10 '21 at 20:50
  • If this doesn't work I'd look at [this questions answer](https://stackoverflow.com/questions/59977856/firebase-listener-with-react-hooks) – dave Oct 10 '21 at 20:51