react-native-community/hooks

UseInteractionManager clean up on component Unmount

nitishmyn opened this issue · 3 comments

With reference to the documentation of https://reactnative.dev/docs/interactionmanager
The method runAfterInteractions returns a cancellable promise, which has a cancel method, was wondering if this cancel method should be applied on component clean up.

https://github.com/react-native-community/hooks/blob/master/src/useInteractionManager.ts#L8

import {useEffect, useState, useRef} from 'react'
import {InteractionManager} from 'react-native'

export function useInteractionManager() {
  const [complete, updateInteractionStatus] = useState(false)
  const interactionHandler = useRef();
  useEffect(() => {
    interactionHandler.current = InteractionManager.runAfterInteractions(() => {
      updateInteractionStatus(true)
      
      
      
    })
    return () => interactionHandler.current & interactionHandler.current.cancel()
  }, [interactionHandler])
  return complete
}

Sound like a good idea!

Although I don't think that the code that you posted works? It needs to return something to the function inside useEffect, not the runAfterInteractions function.

What do you think about the approaching #310? It's the same as in the example code in the documentation you linked

@LinusU Yep sorry that was a mistake while formatting the code on my part.. Yes i intended to do what you meant in #310

Merged 👍