-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Introduce a hook to auto dispose view models #30549
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
* 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.
If we're okay with the view-model returned from this hook being undefined, we could just delay creating the vm to the effect body below. But we would have to always vm && <FooView vm={vm}>
and vm?.doSomething()
.
It would also mean that all views would require an additional render (effect body always runs after render so the vm is only created after one render) before they are mounted. Also for view-models that immediately start doing async operations on instantiation, they would be delayed by one render cycle. But I doubt any of these would have actual performance implications.
I personally favor this approach because it feels equivalent to just calling the vm ctor.
4745e97
to
2b9ee57
Compare
2b9ee57
to
a76654f
Compare
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