Skip to main content

Module: decorators/autobind

Functions

autobind

autobind(target, key, __namedParameters): Object

Description

You can use @autobind and decorate any class method that binds the instance of the current class as its this, it can also be used with @action.

Example

class Shop {
@state
count = 0;

list: string[] = [];

@autobind
@action
increase() {
this.count += 0;
}

@autobind
addGood(text) {
this.list.push(text);
}
}

const app = testBed({
modules: [],
main: Shop,
});

const { increase, addGood } = app.instance;
increase();
addGood('apple');
expect(app.instance.count).toBe(1);
expect(app.instance.list).toEqual(['apple']);

Parameters

NameType
targetobject
keystring | symbol
__namedParametersTypedPropertyDescriptor<any>

Returns

Object

NameType
configurableundefined | boolean
enumerableundefined | boolean
get() => any
set(setValue: unknown) => void

Defined in

packages/reactant-module/src/decorators/autobind.ts:42