I ran into a need of cleaning a useEffect when component is unmounted but with an access to the current props. Like componentWillUnmount can do by getting this.props.whatever
Here is a small example: Click "set count" button 5 times and look at your console. You'll see 0 from the console.log in component B, regardless "count" will be 5 https://codesandbox.io/embed/vibrant-feather-v9f6o
How to implement the behavior of getting current props in useEffect cleanup function (5 in my case)?
UPDATE: Passing count to the dependencies of useEffect won't help because:
- Cleanup will be invoked on every new count is passed to the props
- The last value will be 4 instead of the desired 5