Home > OS >  Button is self-pushing (JS)
Button is self-pushing (JS)

Time:01-14

Logout button class, Im creating instance to connect it with API. After the moment instance created it is immediately activated like the button was pushed Can you help to understand what is wrong? I'm just beginner

class LogoutButton {
    constructor() {
        [this.logoutBtn] = document.getElementsByClassName('logout');
        this.action = (f) => f;
        this.logoutBtn.addEventListener('click', this.logoutClick.bind(this));
    }

    logoutClick(event) {
        event.preventDefault();
        this.action();
    }
}

instance

"use strict"

let newLogoutButton = new LogoutButton();

const logoutSuccess = (data) => {
    if (data.success === true) {
        location.reload();
    } else {
        alert("");
    }
};

newLogoutButton.action = ApiConnector.logout(logoutSuccess);

CodePudding user response:

The last line needs to be a function, right now you're just immediately calling .logout()

newLogoutButton.action = () => ApiConnector.logout(logoutSuccess);

CodePudding user response:

the issue is with the last line newLogoutButton.action = ApiConnector.logout(logoutSuccess);

change it to newLogoutButton.action = () => ApiConnector.logout(logoutSuccess);

  • Related