yuchi / hooks.macro

☔️ Babel Macros for automatic React Hooks memoization invalidation
MIT License
357 stars 5 forks source link

Custom hook dependencies with auto() #14

Closed adrianhelvik closed 4 years ago

adrianhelvik commented 4 years ago

I wrote a basic version of auto(). It can be used like this:

import { auto } from 'hooks.macro'

function MyComponent() {
  useSomeMacro(() => {
    console.log(foo)
  }, auto())
}

turns into

```javascript
import { auto } from 'hooks.macro'

function MyComponent() {
  useSomeMacro(() => {
    console.log(foo)
  }, [foo])
}

I used inline snapshot output to avoid wrestling with Jest. Will update that later.

What do you think?

yuchi commented 4 years ago

Thank you! I love it! Sorry if it not going to merge it straight away but I need to think a little about how well it works in the wild (by looking at actual hooks that accept a dependencies array).

adrianhelvik commented 4 years ago

That's perfectly okay. It's a good idea to not rush API additions.

useAsyncEffect would be a great candidate for using auto(): https://www.npmjs.com/package/use-async-effect

auto() as I implemented it would work well with this as dependencies are retrieved from all function expressions in the hook call. I will publish the updated package at @adrianhelvik/hooks.macro and test things out!

Edit: It's now available at @adrianhelvik/hooks.macro if you want to test it out.

adrianhelvik commented 4 years ago

Hooks usage in the wild seems to be all over the place, and equal function references seems to be the bast pattern imo.

(though auto() is fantastic)