'Observables-hooks How to to subscribe only on click

I am an angular dev who is new to React. I use observables to manage all my states as that is what I am familiar with. I am trying to figure out the best way to write a custom observable hook that only subscribes when the user clicks a button

I am trying to implement something like this:

const addMovieHandler = useCallback((movie) => {
setIsLoading(true);
addMovie$(movie).subscribe((data) => {
  console.log(data);
  fetchMovies()
});
 }, [fetchMovies]);

It works but it seems hacky and I do not know where to put the unsubscribe



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source