salsita / spicy-hooks

7 stars 0 forks source link

Add `core/useDisposable` #34

Closed goce-cz closed 3 years ago

goce-cz commented 3 years ago

Purpose

The purpose of this hooks is to allocate resources synchronously while ensuring that those will be properly disposed whenever the dependencies change or the enclosing component gets unmounted.

Usage example

const portalElement = useDisposable(
  () => {
    const element = window.document.createElement(tagName)
    window.document.body.append(element)
    return [element, () => element.remove()]
  },
  [tagName]
)
goce-cz commented 3 years ago

just a wrapper of the useImmediateEffect hook with a special reference.

Well, you're right that this is a very simple wrapper, but its use-case is clearly defined - unlike the one of useImmediateEffect. The latter hooks is also a bit dangerous, so I think that offering more specific safer variant(s) is not a bad idea.

We can ask other people though: @craig-bishell , @Bartunek , @tomkis , @shkarpa