crimx/observable-hooks

useSubscription closure not updated

Draeken opened this issue · 3 comments

Hello!

I have a similar use of useSubscription which access closure (a prop).

const [debug, setDebug] = useState(false)
const subscription = useSubscription(events$, null, error => {
  if (debug) {
    console.log(error)
  }
})

But when the prop is updated, and useSubscription's event handler is fired, it's always the old prop's value. I could add the prop in the observable used by useSubscription but it's cumbersome. Is there another way?

crimx commented

Hi! Are you using the latest version?

crimx commented

It should work on v2. You can see the test here:

rerender({ propVal: 'p2' })
expect(numSpy).toBeCalledTimes(0)
num$$.next(2)
expect(numSpy).lastCalledWith(2, 's2', 'p2')

If you are using v1 you have to create an extra Observable to track values and also useObservableCallback to track the latest callback. In v2 I've simplify the API with just useSubscription and also with a performance boost.

I was using v1 😮
It's much simpler now, thanks!