Been following the react guide and i want to display the users username on the page like i would in blaze.
From the guide:
In your data container, you can use Meteor.user() to check if a user is logged in and get information about them. For example, Meteor.user().username contains the logged in user’s username. You can also use Meteor.userId() to just get the current user’s _id.
However, i just cant figure out how to DISPLAY the username on the page.
{ this.props.currentUser.username } gives the following error:
Invariant Violation: Objects are not valid as a React child (found: object with keys {_id, username}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of 'App'.
Meteor.user() is a reactive data source. That means it could be null for a bit before your user record is loaded and ready. So within your App component, your currentUser prop has an initial value of null. You can’t reference this.props.currentUser.username until currentUser exists, which is why it works within your this.props.currentUser ? ... check (since that check makes sure the currentUser exists first before referencing the username property).
JS won’t let you try to access a property of undefined or null
Meteor.user() is null if the user isn’t logged in, so this error can appear when the component gets rendered before the user is logged in by the client
So you should check that the object exists or use a default: