usehooks-ts icon indicating copy to clipboard operation
usehooks-ts copied to clipboard

Feature: Add useEffectTill

Open samavati opened this issue 1 year ago • 3 comments

PR Description

Summary

This PR introduces a custom React hook, useEffectTill, designed to run an effect only until it is marked as done, handling cleanup afterward. The hook enhances the standard useEffect by allowing for precise control over when the effect should run and when it should be cleaned up.

Changes Made

  • Added TypeScript type definitions for the callback functions used in the hook.
  • Implemented the useEffectTill hook, including tracking if the effect has already run and handling cleanup.
  • Added internal comments to enhance code readability and understanding.

Usage Example

useEffectTill((done) => {
  if (data) {
    // Do something.
    done();
  }
}, [data]);

Why

The useEffectTill hook fills a gap in the React ecosystem by offering developers a way to execute effects with precise control over their lifecycle. It enhances code readability and reduces potential bugs related to effect cleanup.

How to Test

  1. Apply the useEffectTill hook in a React component.
  2. Write tests to cover scenarios where the effect should and should not run.
  3. Verify that cleanup occurs correctly once the effect is marked as done.

Additional Notes

  • This implementation aligns with best practices in React development.
  • The code includes appropriate TypeScript typings for a seamless integration into TypeScript projects.

samavati avatar Nov 28 '23 17:11 samavati

⚠️ No Changeset found

Latest commit: 5df394e57937add3ba1a6b761ec75682c967604c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

changeset-bot[bot] avatar Nov 28 '23 17:11 changeset-bot[bot]

Someone is attempting to deploy a commit to a Personal Account owned by @juliencrn on Vercel.

@juliencrn first needs to authorize it.

vercel[bot] avatar Nov 28 '23 17:11 vercel[bot]

I think useEffectUntil is a better name if this is being added. "Til" is a contraction of "until" anyway (and mis-spelt as "till" here).

sambauers avatar Jan 27 '24 03:01 sambauers