Skip to content

Conversation

MidhunSureshR
Copy link
Member

@MidhunSureshR MidhunSureshR commented Aug 12, 2025

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:

const vm = useAutoDisposedViewModel(() => new FooViewModel(...));

Which would be the equivalent of

const vm = new FooViewModel(...);

* 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);
Copy link
Member Author

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.

@MidhunSureshR MidhunSureshR force-pushed the midhun/mvvm/state-management-4 branch from 4745e97 to 2b9ee57 Compare August 23, 2025 14:21
@MidhunSureshR MidhunSureshR changed the base branch from midhun/mvvm/state-management-3 to midhun/mvvm/state-management-2 August 23, 2025 14:21
@MidhunSureshR MidhunSureshR added T-Task Tasks for the team like planning and removed T-Task Tasks for the team like planning labels Aug 23, 2025
@MidhunSureshR MidhunSureshR changed the base branch from midhun/mvvm/state-management-2 to develop August 23, 2025 14:27
@MidhunSureshR MidhunSureshR changed the base branch from develop to midhun/mvvm/state-management-2 August 23, 2025 14:27
@MidhunSureshR MidhunSureshR changed the base branch from midhun/mvvm/state-management-2 to develop August 23, 2025 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-Task Tasks for the team like planning
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant