Skip to content

Conversation

@MidhunSureshR
Copy link
Member

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(...);

@@ -0,0 +1,47 @@
/*
Copyright 2025 New Vector Ltd.
Copy link
Member

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
Copy link
Member

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

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants