resolve undefined this in onClick handler (#32576)

* 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
This commit is contained in:
Zack
2026-02-19 14:55:52 +01:00
committed by GitHub
parent d597fc61d6
commit 134c7cde46
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>