❌ Wrong
```
---
## Scope Issues
### Target Outside Controller Scope
```html
✅ Inside scope
Content
❌ Outside scope - won't work
```
### Nested Controller Confusion
```html
❌ Child blocks parent's access
✅ Correct scope
```
Parent cannot see targets inside nested child controller scope.
---
## Lifecycle Timing
### Accessing Targets Too Early
```javascript
export default class extends Controller {
static targets = ['output'];
initialize() {
this.outputTarget.textContent = 'Hello'; ❌ Targets not ready
}
connect() {
this.outputTarget.textContent = 'Hello'; ✅ Targets available
}
}
```
### Not Cleaning Up
```javascript
export default class extends Controller {
connect() {
this.interval = setInterval(() => this.tick(), 1000);
window.addEventListener('resize', this.handleResize); ❌ Leaks
}
// Missing disconnect() - causes memory leaks with Turbo!
disconnect() {
clearInterval(this.interval);
window.removeEventListener('resize', this.handleResize); ✅
}
}
```
### Binding `this` for Event Listeners
```javascript
export default class extends Controller {
connect() {
// ❌ Wrong - `this` will be wrong when called
window.addEventListener('resize', this.handleResize);
// ✅ Correct - bind or use arrow function
this.boundResize = this.handleResize.bind(this);
window.addEventListener('resize', this.boundResize);
}
disconnect() {
window.removeEventListener('resize', this.boundResize);
}
}
```
---
## Value Gotchas
### Values Are Strings in HTML
```html
```
Stimulus converts based on type declaration:
```javascript
static values = {
count: Number, // Converted to number
enabled: Boolean // "true"/"false" converted to boolean
}
```
### Boolean Values
```html
✅ Works
✅ Works
❌ Empty string, not false
```
### Array/Object Values Need Valid JSON
```html
✅ Valid JSON
✅ Also works
✅ Valid JSON
❌ Invalid JSON
❌ May produce invalid JSON
✅ Twig
```
### Value Changed Fires on Connect
```javascript
countValueChanged(value, previousValue) {
// On connect: previousValue is undefined
if (previousValue === undefined) return; // Skip initial call if needed
}
```
---
## Action Gotchas
### Forgetting Event Parameter
```javascript
// ❌ Can't access event
submit() {
// event is undefined
}
// ✅ Correct
submit(event) {
event.preventDefault();
}
```
### Action Parameters Are Always Strings
```html