Skip to content

Commit a67b9c0

Browse files
committed
fix(useConditionalEffect): add overloads with generic types
fix #14
1 parent 80ea3d0 commit a67b9c0

File tree

1 file changed

+150
-3
lines changed

1 file changed

+150
-3
lines changed

src/index.tsx

Lines changed: 150 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,148 @@ export function useEffectUpdate<Dependencies extends readonly any[]>(
227227
}, dependencies);
228228
}
229229

230+
/**
231+
* Conditionally executes an effect.
232+
*
233+
* @param evalCondition A function that receives the dependencies' previous state as argument and
234+
* returns a boolean defining if the effect should be executed.
235+
* @param effect The effect callback to be executed.
236+
* @param dependencies The effect's dependencies.
237+
*/
238+
export function useConditionalEffect<T1>(
239+
evalCondition: (oldState: [T1]) => boolean,
240+
effect: () => (() => void) | void,
241+
dependencies: [T1],
242+
): void;
243+
244+
/**
245+
* Conditionally executes an effect.
246+
*
247+
* @param evalCondition A function that receives the dependencies' previous state as argument and
248+
* returns a boolean defining if the effect should be executed.
249+
* @param effect The effect callback to be executed.
250+
* @param dependencies The effect's dependencies.
251+
*/
252+
export function useConditionalEffect<T1, T2>(
253+
evalCondition: (oldState: [T1, T2]) => boolean,
254+
effect: () => (() => void) | void,
255+
dependencies: [T1, T2],
256+
): void;
257+
258+
/**
259+
* Conditionally executes an effect.
260+
*
261+
* @param evalCondition A function that receives the dependencies' previous state as argument and
262+
* returns a boolean defining if the effect should be executed.
263+
* @param effect The effect callback to be executed.
264+
* @param dependencies The effect's dependencies.
265+
*/
266+
export function useConditionalEffect<T1, T2, T3>(
267+
evalCondition: (oldState: [T1, T2, T3]) => boolean,
268+
effect: () => (() => void) | void,
269+
dependencies: [T1, T2, T3],
270+
): void;
271+
272+
/**
273+
* Conditionally executes an effect.
274+
*
275+
* @param evalCondition A function that receives the dependencies' previous state as argument and
276+
* returns a boolean defining if the effect should be executed.
277+
* @param effect The effect callback to be executed.
278+
* @param dependencies The effect's dependencies.
279+
*/
280+
export function useConditionalEffect<T1, T2, T3, T4>(
281+
evalCondition: (oldState: [T1, T2, T3, T4]) => boolean,
282+
effect: () => (() => void) | void,
283+
dependencies: [T1, T2, T3, T4],
284+
): void;
285+
286+
/**
287+
* Conditionally executes an effect.
288+
*
289+
* @param evalCondition A function that receives the dependencies' previous state as argument and
290+
* returns a boolean defining if the effect should be executed.
291+
* @param effect The effect callback to be executed.
292+
* @param dependencies The effect's dependencies.
293+
*/
294+
export function useConditionalEffect<T1, T2, T3, T4, T5>(
295+
evalCondition: (oldState: [T1, T2, T3, T4, T5]) => boolean,
296+
effect: () => (() => void) | void,
297+
dependencies: [T1, T2, T3, T4, T5],
298+
): void;
299+
300+
/**
301+
* Conditionally executes an effect.
302+
*
303+
* @param evalCondition A function that receives the dependencies' previous state as argument and
304+
* returns a boolean defining if the effect should be executed.
305+
* @param effect The effect callback to be executed.
306+
* @param dependencies The effect's dependencies.
307+
*/
308+
export function useConditionalEffect<T1, T2, T3, T4, T5, T6>(
309+
evalCondition: (oldState: [T1, T2, T3, T4, T5, T6]) => boolean,
310+
effect: () => (() => void) | void,
311+
dependencies: [T1, T2, T3, T4, T5, T6],
312+
): void;
313+
314+
/**
315+
* Conditionally executes an effect.
316+
*
317+
* @param evalCondition A function that receives the dependencies' previous state as argument and
318+
* returns a boolean defining if the effect should be executed.
319+
* @param effect The effect callback to be executed.
320+
* @param dependencies The effect's dependencies.
321+
*/
322+
export function useConditionalEffect<T1, T2, T3, T4, T5, T6, T7>(
323+
evalCondition: (oldState: [T1, T2, T3, T4, T5, T6, T7]) => boolean,
324+
effect: () => (() => void) | void,
325+
dependencies: [T1, T2, T3, T4, T5, T6, T7],
326+
): void;
327+
328+
/**
329+
* Conditionally executes an effect.
330+
*
331+
* @param evalCondition A function that receives the dependencies' previous state as argument and
332+
* returns a boolean defining if the effect should be executed.
333+
* @param effect The effect callback to be executed.
334+
* @param dependencies The effect's dependencies.
335+
*/
336+
export function useConditionalEffect<T1, T2, T3, T4, T5, T6, T7, T8>(
337+
evalCondition: (oldState: [T1, T2, T3, T4, T5, T6, T7, T8]) => boolean,
338+
effect: () => (() => void) | void,
339+
dependencies: [T1, T2, T3, T4, T5, T6, T7, T8],
340+
): void;
341+
342+
/**
343+
* Conditionally executes an effect.
344+
*
345+
* @param evalCondition A function that receives the dependencies' previous state as argument and
346+
* returns a boolean defining if the effect should be executed.
347+
* @param effect The effect callback to be executed.
348+
* @param dependencies The effect's dependencies.
349+
*/
350+
export function useConditionalEffect<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
351+
evalCondition: (oldState: [T1, T2, T3, T4, T5, T6, T7, T8, T9]) => boolean,
352+
effect: () => (() => void) | void,
353+
dependencies: [T1, T2, T3, T4, T5, T6, T7, T8, T9],
354+
): void;
355+
356+
/**
357+
* Conditionally executes an effect.
358+
*
359+
* @param evalCondition A function that receives the dependencies' previous state as argument and
360+
* returns a boolean defining if the effect should be executed.
361+
* @param effect The effect callback to be executed.
362+
* @param dependencies The effect's dependencies.
363+
*/
364+
export function useConditionalEffect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(
365+
evalCondition: (
366+
oldState: [T1, T2, T3, T4, T5, T6, T7, T8, T9, T10],
367+
) => boolean,
368+
effect: () => (() => void) | void,
369+
dependencies: [T1, T2, T3, T4, T5, T6, T7, T8, T9, T10],
370+
): void;
371+
230372
/**
231373
* Conditionally executes an effect.
232374
*
@@ -238,16 +380,21 @@ export function useEffectUpdate<Dependencies extends readonly any[]>(
238380
* the effect is evaluating if it should be executed.
239381
* @category Effects
240382
*/
241-
export const useConditionalEffect = <Dependencies extends readonly any[]>(
383+
export function useConditionalEffect<Dependencies extends readonly any[]>(
242384
evalCondition: (oldState: Dependencies) => boolean,
243385
effect: () => (() => void) | void,
244386
dependencies: Dependencies,
245-
) => {
387+
): void;
388+
export function useConditionalEffect<Dependencies extends readonly any[]>(
389+
evalCondition: (oldState: Dependencies) => boolean,
390+
effect: () => (() => void) | void,
391+
dependencies: Dependencies,
392+
): void {
246393
useEffectUpdate(
247394
oldState => (evalCondition(oldState) ? effect() : undefined),
248395
dependencies,
249396
);
250-
};
397+
}
251398

252399
/**
253400
* Runs an effect when the component gets mounted.

0 commit comments

Comments
 (0)