resolve undefined this in onClick handler (#32576) (#32583)

* resolve undefined this in onClick handler

* add regression test for bound DisambiguatedProfileViewModel onClick

* Update docs

* add regression for sender profile click mention insertion

* Eslint Fix

(cherry picked from commit 134c7cde46)

Co-authored-by: Zack <zazi21@student.bth.se>
This commit is contained in:
ElementRobot
2026-02-19 16:10:05 +00:00
committed by GitHub
co-authored by Zack
parent faf3278a8e
commit d19208bee3
4 changed files with 59 additions and 3 deletions
+14
View File
@@ -127,6 +127,20 @@ export class FooViewModel extends BaseViewModel<FooViewSnapshot, Props> implemen
}
```
#### Binding of View Model Actions:
All view model actions must be defined as arrow functions to ensure they are bound to the class instance.
Using standard class methods can result in `this` being undefined when the function is passed as a callback (e.g. to a React event handler), which may cause runtime errors.
Correct pattern:
```ts
public doSomething = (): void => {
...
};
```
### `useViewModel` hook
Your view must call this hook with the view-model as argument. Think of this as your view subscribing to the view model.<br>