-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve
useConditionalEffect
and remove `useConditionalUpdate…
…Effect` (#345) * feat: align `useConditionalEffect` signature with `useEffect` BREAKING CHANGE: `useConditionalEffect` conditions and deps arguments now switched places. * feat(useConditionalEffect): added ability to wrap other effect hooks * feat: remove `useConditionalUpdateEffect` BREAKING CHANGE: `useConditionalUpdateEffect` removed in favor of composition with `useConditionalEffect`. Now you should simpy pass extra argument to achieve same functionality: `useConditionalEffect(()=>{}, undefined, [], truthyAndArrayPredicate, useUpdateEffect)` BREAKING CHANGE: Interface `IUseConditionalEffectPredicate` renamed to `IConditionsPredicate` * docs: cleanup, remove `useConditionalUpdateEffect` from readme Co-authored-by: Joe Duncko <[email protected]>
- Loading branch information
Showing
15 changed files
with
130 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,50 @@ | ||
import { DependencyList, EffectCallback, useEffect } from 'react'; | ||
import { truthyAndArrayPredicate } from '..'; | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { DependencyList, useEffect } from 'react'; | ||
import { IEffectCallback, IEffectHook, truthyAndArrayPredicate } from '..'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type IUseConditionalEffectPredicate<Cond extends ReadonlyArray<any>> = ( | ||
export type IConditionsList = ReadonlyArray<any>; | ||
|
||
export type IConditionsPredicate<Cond extends IConditionsList = IConditionsList> = ( | ||
conditions: Cond | ||
) => boolean; | ||
|
||
/** | ||
* Like `useEffect` but callback invoked only if conditions match predicate. | ||
* | ||
* @param callback Callback to invoke | ||
* @param conditions Conditions array | ||
* @param deps Dependencies list like for `useEffect`. If set - effect will be | ||
* @param callback Function that will be passed to underlying effect hook. | ||
* @param deps Dependencies list like for `useEffect`. If not undefined - effect will be | ||
* triggered when deps changed AND conditions are satisfying predicate. | ||
* @param conditions Conditions array. | ||
* @param predicate Predicate that defines whether conditions satisfying certain | ||
* provision. By default, it is all-truthy provision, meaning that all | ||
* conditions should be truthy. | ||
* @param effectHook Effect hook that will be used to run callback. Must comply `useEffect` | ||
* signature, meaning that callback should be placed as first argument and dependencies list | ||
* as second. | ||
* @param effectHookRestArgs Extra arguments that are passed to `effectHook`. | ||
*/ | ||
export function useConditionalEffect<T extends ReadonlyArray<unknown>>( | ||
callback: EffectCallback, | ||
conditions: T, | ||
deps?: DependencyList, | ||
predicate: IUseConditionalEffectPredicate<T> = truthyAndArrayPredicate | ||
export function useConditionalEffect< | ||
Cond extends IConditionsList, | ||
Callback extends IEffectCallback = IEffectCallback, | ||
Deps extends DependencyList | undefined = DependencyList | undefined, | ||
HookRestArgs extends any[] = any[], | ||
R extends HookRestArgs = HookRestArgs | ||
>( | ||
callback: Callback, | ||
deps: Deps, | ||
conditions: Cond, | ||
predicate: IConditionsPredicate<Cond> = truthyAndArrayPredicate, | ||
effectHook: IEffectHook<Callback, Deps, HookRestArgs> = useEffect, | ||
...effectHookRestArgs: R | ||
): void { | ||
// eslint-disable-next-line consistent-return | ||
useEffect(() => { | ||
if (predicate(conditions)) { | ||
return callback(); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, deps); | ||
effectHook( | ||
// eslint-disable-next-line consistent-return | ||
(() => { | ||
if (predicate(conditions)) { | ||
return callback(); | ||
} | ||
}) as Callback, | ||
deps, | ||
...effectHookRestArgs | ||
); | ||
} |
37 changes: 0 additions & 37 deletions
37
src/useConditionalUpdateEffect/__docs__/example.stories.tsx
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.