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:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user