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?