Mohamad Amjad
Mohamad Amjad

Reputation: 19

Can not render different results with conditional rendering

I am trying to check if the user has a username to allow him to upload images otherwise i want to ask the user to login but i try to do that this error appears :

TypeError: Cannot read property 'displayName' of null

App
C:/Users/User/Desktop/try-react/new-app/src/App.js:91

this it the code which causes the error:

return (
    <div className="app">
 
        {user.displayName  ? <ImageUpload  username={user.displayName} /> : <h3>login</h3>}
        

Upvotes: 1

Views: 79

Answers (1)

Anup
Anup

Reputation: 629

Your user has null value. Use this for null check

{user ? <ImageUpload  username={user?.displayName} /> : <h3>login</h3>}

Upvotes: 1

Related Questions