react-use icon indicating copy to clipboard operation
react-use copied to clipboard

feat: add support for a cleanup function in useInterval

Open RJWadley opened this issue 2 years ago • 1 comments

Description

This change updates the useInterval hook to allow the callback function to optionally return a cleanup function. The cleanup function, if provided, will be called between intervals and on unmount, much like the cleanup function in useEffect.

This change adds more flexibility to the useInterval hook, allowing users to more easily perform additional cleanup or tear-down logic as needed. This makes it easier to work with async code and event listeners.

Type of change

  • [ ] Bug fix (non-breaking change which fixes an issue)
  • [x] New feature (non-breaking change which adds functionality)
  • [x] Breaking change (fix or feature that would cause existing functionality to not work as before)

Checklist

  • [x] Read the Contributing Guide
  • [x] Perform a code self-review
  • [x] Comment the code, particularly in hard-to-understand areas
  • [x] Add documentation
  • [x] Add hook's story at Storybook
  • [x] Cover changes with tests
  • [x] Ensure the test suite passes (yarn test)
  • [x] Provide 100% tests coverage
  • [x] Make sure code lints (yarn lint). Fix it with yarn lint:fix in case of failure.
  • [x] Make sure types are fine (yarn lint:types).

RJWadley avatar Mar 08 '23 20:03 RJWadley

This change is breaking because the type signature changed from useInterval(callback: Function, delay?: number | null) to useInterval(callback: () => VoidFunction | void, delay?: number | null)

here's an example that would break:

const sampleCallback = () => "Hello!"
useInterval(sampleCallback)

This was never technically correct, but the type was relaxed enough to allow it.

RJWadley avatar Mar 08 '23 21:03 RJWadley