sajad torkamani
useEffect(() => {
  const subscription = props.source.subscribe()
  return () => {
    // Clean up the subscription
    subscription.unsubscribe()
  }
})

If you return a function from your useEffect hook, React will call this function before the component is removed from the DOM. If a component re-renders (as they typically do), the previous effect is cleaned up before executing the next effect.

Image credit: donavan

Leave a comment

Your email address will not be published. Required fields are marked *