---
title: "@bound"
description: "Automatically bind methods to the class instance."
order: 14
---

import { RuiAlert, RuiAlertDescription, RuiAlertTitle } from '@ecopages/radiant-ui/alert';



# @bound

The `@bound` decorator automatically binds a method to its class instance, ensuring `this` always refers to the component. This is particularly useful when passing methods as callbacks to event listeners, timers, or callback-style APIs.

## Usage

```typescript
import { RadiantElement, bound, customElement } from '@ecopages/radiant';

@customElement('timer-component')
export class TimerComponent extends RadiantElement {
	private count = 0;

	@bound
	increment() {
		this.count++;
		console.log(this.count);
	}

	override connectedCallback() {
		super.connectedCallback();
		setTimeout(this.increment, 1000);
	}
}
```

## Common Use Cases

### Event Listeners

```typescript
@customElement('scroll-tracker')
export class ScrollTracker extends RadiantElement {
	@bound
	handleScroll() {
		console.log(`Scrolled to: ${window.scrollY}px`);
	}

	override connectedCallback() {
		super.connectedCallback();
		window.addEventListener('scroll', this.handleScroll);
	}

	override disconnectedCallback() {
		window.removeEventListener('scroll', this.handleScroll);
	}
}
```

### Promise Callbacks

```typescript
@customElement('data-loader')
export class DataLoader extends RadiantElement {
	data: unknown[] = [];

	@bound
	handleData(data: unknown[]) {
		this.data = data;
	}

	async loadData() {
		fetch('/api/data')
			.then((response) => response.json())
			.then(this.handleData);
	}
}
```

## @bound vs Arrow Functions

Arrow functions also capture `this`. A `@bound` method stays on the prototype, so a subclass can override it, and each instance gets its own bound copy: when the instance is created under standard decorators, on first access under legacy decorators. Standard decorators throw when `@bound` decorates a `#private` method.

```typescript
@customElement('my-component')
export class MyComponent extends RadiantElement {
	@bound
	boundMethod() {}

	arrowMethod = () => {}
}
```

<RuiAlert variant="success" layout="banner" class="unstyled">
  <RuiAlertTitle>TIP</RuiAlertTitle>
  <RuiAlertDescription>
    <p>Use <code>@bound</code> for methods that may be passed around, removed later with the same identity, or overridden in a subclass.</p>
  </RuiAlertDescription>
</RuiAlert>

## Learn More

- [@onEvent](/docs/decorators/on-event) - Declarative event subscription for DOM events.
- [@debounce](/docs/decorators/debounce) - Often combined with <code>@bound</code> when handlers are both passed around and delayed.