Hi everyone!
I’m having trouble with change detection in a Glimmer component and could use some guidance. When I mutate properties on objects within a `@tracked` array, the template doesn’t re-render.
**My Setup:**
```javascript
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { fn } from '@ember/helper';
import { on } from '@ember/modifier';
import { action } from '@ember/object';
export default class CustomSidebarComponent extends Component {
@tracked items = [
{
id: 'home',
label: 'Home',
expanded: false
},
{
id: 'my-posts',
label: 'My Posts',
expanded: false
}
// ... more items
];
@action
toggleExpanded(item) {
item.expanded = !item.expanded; // This mutation doesn't trigger re-render
console.log('Toggled:', item.label, item.expanded); // Logs correctly
}
<template>
<div id="custom-sidebar">
<ul>
{{#each this.items key="@index" as |item|}}
<li class="menu-item {{if item.expanded 'expanded'}}" {{on "click" (fn this.toggleExpanded item)}}>
{{item.label}} {{if item.expanded "(expanded)" ""}}
</li>
{{/each}}
</ul>
</div>
</template>
}
```
**The Problem:**
1. The click handler executes correctly
2. The property `item.expanded` gets updated (verified in console)
3. But the template doesn’t re-render - no class changes, no text changes
**What I’ve Tried:**
1. **Array reassignment** - `this.items = […this.items]` after mutation (doesn’t work)
2. **Immer for immutable updates** - Wanted to try this but can’t get npm imports to work in Discourse themes
3. **Different key strategies** - `key=“id”` instead of `key=“@index”`
**Questions:**
- Is mutating properties on objects within `@tracked` arrays supposed to work in Glimmer?
- Do I need to make individual objects tracked somehow?
- Is there a recommended pattern for this use case?
- Could this be related to running in a Discourse theme environment?
- Are there limitations with npm imports in Discourse themes that affect reactivity libraries?
**Environment:**
- Discourse theme component
- Glimmer components with `.gjs` files
- Latest Discourse version -updated today
Any insights would be greatly appreciated! Is there something fundamental I’m missing about Glimmer’s reactivity system?
Thanks!