Skip to content

Commit

Permalink
Fix effect type
Browse files Browse the repository at this point in the history
  • Loading branch information
UpperCod committed Mar 7, 2024
1 parent ccae664 commit 3ba0e1c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
8 changes: 8 additions & 0 deletions types/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ export const useEffect: Hooks.UseEffect;
*/
export const useLayoutEffect: Hooks.UseLayoutEffect;

/**
* Evaluate the execution of a callback after each render cycle,
* if the arguments between render do not change the callback
* will not be executed, If the callback returns a function
* it will be executed as an effect collector
*/
export const useRefEffect: Hooks.UseRefEffect;

/**
* Lets you use the redux pattern as Hook
*/
Expand Down
12 changes: 9 additions & 3 deletions types/hooks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ export type UseState = <OptionalInitialState = any>(
initialState?: OptionalInitialState
) => ReturnUseState<GetInitialState<OptionalInitialState>>;

type EffectCallback = () => void | (() => any);
/**
* UseEffect
*/
export type UseEffect = <Effect extends () => void | (() => any)>(
effect: Effect,
args?: any[]
export type UseEffect = <Args = any>(
effect: EffectCallback,
args?: Args[]
) => void;

/**
Expand All @@ -52,6 +53,11 @@ export type UseLayoutEffect = UseEffect;
*/
export type UseInsertionEffect = UseEffect;

/**
* UseLayoutEffect
*/
export type UseRefEffect = (effect: EffectCallback, args: Ref<any>[]) => void;

/**
* UseMemo
*/
Expand Down
11 changes: 11 additions & 0 deletions types/tests/28-use-effect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useEffect, useRefEffect, createRef } from "core";

useEffect(() => () => {});
useEffect(() => {}, []);
useEffect<number>(() => {}, [1, 2, 3]);

const r = createRef(10);

useRefEffect(() => {
r.current.toFixed(10);
}, [r]);

0 comments on commit 3ba0e1c

Please sign in to comment.