Reputation:
I'm trying to use the user function from that.
class GlobalParams {
constructor() {
this.user = '';
}
user = (user) => {
if(!user) return this.user;
this.user = user;
}
}
export default gp = new GlobalParams();
And use it in state as - user: gp.user(), but it says that 'user' is not exported.
Request:
getUserData() {
fetch(`${API}/auth/user`, {
method: 'GET',
withCredentials: true,
credentials: 'include'
})
.then (response => response.json())
.then (response => {
gp.setUser(response)
})
.catch (error => {
console.error (error);
});
}
Upvotes: 0
Views: 51
Reputation: 1325
You have duplicate property names in this class.
In the constructor, you define this.user
as an empty string.
Then, on the line user = (user) => {
you basically assign a function to this.user
.
But, the constructor is called only when you do new GlobalParams()
, so in the end this.user
will be an empty string, not a function.
The solution: just change the name of the method:
class GlobalParams {
constructor() {
this.user = "";
}
setUser = (user) => {
if (!user) return this.user;
this.user = user;
};
}
export default new GlobalParams();
Now you would use it like this:
import gp from "./GlobalParams";
gp.user // to get the user
gp.setUser("new user"); // to set the user
Upvotes: 1