this jest niezdefiniowane w metodzie

0

Czołem,
W aplikacji z Reactem mam taką klasę

export const BooksManipulator = class {
    constructor(state) {
        this.state = state;        
    }

    _remove(id) {
        this.state.data = tableHelpers.remove([...this.state.data], id);
        return this;
    }

    _filtrate() {
        tableHelpers.filtrate(this.state.data, this.state.filter);
        return this;
    }
   
   // ...dużo innych podobnych metod...

    Remove(payload) {
        this._remove(payload).this._filtrate().this._sort().this._changePage().this._setNumberOfPages();
    }
};

Użyta nazwa state może być trochę myląca, wynika to z tego, że ma to wspierać działanie reducera i konstruktor przyjmuje jako argument faktyczny state reducera. Idea jest taka, że definiuję kilka podstawowych metod (nazwy zaczynają się od _) oraz metody zbiorcze, które będą te podstawowe odpowiednio składały. Proawdopodobnie jest to miejsce na TS i klasy abstrakcyjne ale na razie chcę wyłacznie doprowadzić do działania wersję jaka jest.
Wywoływana jest tak

.addCase(removeBook, (state, action) => {
    const manipulator = new BooksManipulator(state);
    manipulator.Remove(action.payload.id);
    state.data = manipulator.state.data;
    state.currentPageBooksData = manipulator.state.currentPageBooksData;
    state.numberOfPages = manipulator.state.numberOfPages;
})

A problem jest taki, że kompiluje się poprawnie, ale w runtime rzuca błąd

Uncaught TypeError: this._remove(...).this is undefined

Próbowałem to związać w konstruktorze, ale bez efektu

constructor(state) {
    this.state = state;
    this._remove = this._remove.bind(this);
}

Ktoś mógłby mi z tym pomóc?

1
  this._remove(payload).this._filtrate().this._sort().this._changePage().this._setNumberOfPages();

Tylko na początku potrzebujesz this. Przecież i tak zwracasz this:

_remove(id) {
   // ....
   return this
}

więc
this._remove(payload)
zwróci ci this, więc wtedy piszesz:

  this._remove(payload)._filtrate()._sort()._changePage()._setNumberOfPages();

na tym polega chaining.

1 użytkowników online, w tym zalogowanych: 0, gości: 1