I would not use the state of the component, as this can be difficult to manage and can lead to problems that are difficult to fix.
You must use cookies or localStorage to store user session data. You can also use closure as a wrapper for your cookie or localStorage .
Here is a simple example of a UserProfile closure that will contain the username.
var UserProfile = (function() { var full_name = ""; var getName = function() { return full_name;
When a user logs in, you can populate this object with a username, email address, etc.
import UserProfile from './UserProfile'; UserProfile.setName("Some Guy");
Then you can get this data from any component in the application, when necessary.
import UserProfile from './UserProfile'; UserProfile.getName();
Using a closure allows you to save data outside the global namespace and make it easily accessible from anywhere in your application.
source share