-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Introduce a hook to auto dispose view models #31178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
d0afe23 to
da048d1
Compare
| @@ -0,0 +1,47 @@ | |||
| /* | |||
| Copyright 2025 New Vector Ltd. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this probably should be Element Creations? (Also could do with a new line below)
| }; | ||
|
|
||
| // eslint-disable-next-line react-compiler/react-compiler | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which dep are we deliberately not including and why?
| * We want to be sure that whatever react component called this hook gets re-rendered | ||
| * when this happens, hence the state. | ||
| */ | ||
| const [viewModel, setViewModel] = useState<B>(vmCreator); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would a useRef be more appropriate maybe, since the view model isn't a thing that would need a re-render when it changes and so isn't really state?
Introduces a hook that ties the lifecycle of the view-model to that of the calling react component.
We follow a bottom up approach in our MVVM refactors. As a consequence, we will have to create view-models in react components until we're done fully moving the code to our new architecture. This introduces the problem of how the view-models should be marked as disposed (i.e who actually calls
vm.dispose()).With this hook, you would:
Which would be the equivalent of