Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Rename useObservableEffect to useDisposable #28

Closed
danielkcz opened this issue Nov 20, 2018 · 13 comments
Closed

Rename useObservableEffect to useDisposable #28

danielkcz opened this issue Nov 20, 2018 · 13 comments

Comments

@danielkcz
Copy link
Collaborator

@xaviergonz I just realized that useObservableEffect might not be needed. The only benefit I see is that you get the disposer to the outer scope, but I am not sure about a single use case scenario where that could be useful (yet).

This will work exactly the same if I am not mistaken since reaction/autorun/when are all returning a function that will be called on unmount anyway. Having a memoized ref to that disposer is such a tiny bonus.

   useEffect(() =>
       reaction(
           () => [props.firstName, props.lastName],
           () => {
               // send this to some server
           }
       )
   , [])

It's not like it's completely useless, but feels like it might confuse users that there is some other benefit to it. Correct me, if I am wrong, please.

@xaviergonz
Copy link
Contributor

doesn't use Effect run for the first time after the first render, layout and paint is finished?

@xaviergonz
Copy link
Contributor

The closest would be useLayoutEffect which is kind of like componentdidmount, but I think people would expect it to run ASAP if they specify things such as fireImmediately.

Could be written some other way? Sure, but I think it is fair to give the users a default implementation so they don't fall in those kind of traps

@danielkcz
Copy link
Collaborator Author

@xaviergonz I gave some thought to this and you are right, that underlying useMemo helps to have an observable effect setup as soon as possible, so that's a good thing.

However, I think that it's also confusing naming. I know, it was my suggestion instead of useMobXEffect, but I haven't seen it before.

In the essence that hook not really specific to MobX. I think a name like useDisposable could be better and revert use of type IReactionDisposer which I have suggested 🙈. What I mean is that it can be a rather universal hook to handle anything that returns a dispose function.

@danielkcz danielkcz changed the title Is useObservableEffect useful at all? Rename useObservableEffect to useDisposable Nov 26, 2018
@danielkcz danielkcz added help wanted Extra attention is needed good first issue Good for newcomers labels Nov 26, 2018
@danielkcz
Copy link
Collaborator Author

As the example goes, this is also useful in MobX world, but due to TypeScript it's not compatible with Lambda type returned by observe.

const state = useObservable(new Map())
useObservableEffect(() => state.observe((change) => ... ))

@danielkcz danielkcz removed good first issue Good for newcomers help wanted Extra attention is needed labels Nov 26, 2018
@danielkcz
Copy link
Collaborator Author

Well, since I essentially needed that last example I just went and did rename it and released in 0.3.3. The useObservableEffect is still there, just using useDisposable underneath.

@xaviergonz
Copy link
Contributor

Why not remove useObservableEffect? It is still v0

@danielkcz
Copy link
Collaborator Author

I know, but I do use in various places and don't want to handle right now 😎

@mweststrate
Copy link
Member

Random curiosity, did anybody encounter cases where useEffect wouldn't have sufficed?

@danielkcz
Copy link
Collaborator Author

danielkcz commented Mar 22, 2019

I am using useDisposable fairly often when I need to imperatively execute some side effect based on a reaction. I even have a useReaction abstraction to simplify these things. To replace that with useEffect I would have to specify dependencies manually and make sure I have observer HOC (won't work with other two variants). Not even mentioning it feels like overkill to re-render the whole component just to execute some side-effect :)

@mweststrate
Copy link
Member

mweststrate commented Mar 22, 2019 via email

@danielkcz
Copy link
Collaborator Author

Ah, I see what you mean now. That's curious :) We have fairly non-trivial logic in the useDisposable implemented by @xaviergonz in #46. There is a tiny benefit that it returns a disposer, but honestly, I haven't used that once, so it's probably useless. I guess we got a little overboard with that one without thinking in basics.

@danielkcz
Copy link
Collaborator Author

Out of curiosity, I tried to strip down the implementation of useDisposable to the useEffect call and only a single test checking for that returned disposer fails. I guess we can easily deprecate this helper with a hint to apply useEffect instead. Thanks, @mweststrate, that was interesting eye opener :)

@mweststrate
Copy link
Member

mweststrate commented Mar 22, 2019 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants