0

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);
                });
            }

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

qp.user returns empty string, can you check my request in the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.