react#EffectCallback TypeScript Examples
The following examples show how to use
react#EffectCallback.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Change.effect.ts From ke with MIT License | 7 votes |
export function useChangeEffect(callback: EffectCallback, deps?: DependencyList): void {
const isFirstRun = useRef(true)
useEffect(() => {
if (isFirstRun.current) {
isFirstRun.current = false
return
}
callback()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps)
}
Example #2
Source File: Pre.effect.ts From ke with MIT License | 6 votes |
export function usePreEffect(callback: EffectCallback, deps?: DependencyList): boolean {
const hasFirstRun = useRef(false)
useEffect(() => {
hasFirstRun.current = true
callback()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps)
return hasFirstRun.current
}
Example #3
Source File: react-helpers.ts From project-loved-web with MIT License | 6 votes |
export function useEffectExceptOnMount(effect: EffectCallback, deps?: DependencyList): void {
const firstUpdate = useRef(true);
useEffect(() => {
if (firstUpdate.current) {
firstUpdate.current = false;
return;
}
return effect();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps);
}
Example #4
Source File: useThrottleEffect.ts From use-antd-resizable-header with MIT License | 6 votes |
function useThrottleEffect(effect: EffectCallback, deps?: DependencyList, options?: Options) {
const [flag, setFlag] = useState({});
const { run, cancel } = useThrottleFn(() => {
setFlag({});
}, options);
useEffect(() => {
return run();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps);
// eslint-disable-next-line react-hooks/exhaustive-deps
React.useEffect(cancel, []);
useUpdateEffect(effect, [flag]);
}
Example #5
Source File: useUpdateEffect.ts From usehooks-ts with MIT License | 6 votes |
function useUpdateEffect(effect: EffectCallback, deps?: DependencyList) {
const isFirst = useIsFirstRender()
useEffect(() => {
if (!isFirst) {
return effect()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps)
}
Example #6
Source File: useUpdateEffect.ts From lucide with ISC License | 6 votes |
useUpdateEffect = (effect: EffectCallback, deps?: DependencyList) => {
const isFirstRef = useRef(true);
if (isFirstRef.current) {
isFirstRef.current = false;
}
useEffect(() => {
if (!isFirstRef.current) {
return effect();
}
}, deps);
}
Example #7
Source File: index.ts From fe-v5 with Apache License 2.0 | 6 votes |
useDeepCompareEffect = (effect: EffectCallback, deps: DependencyList) => {
if (process.env.NODE_ENV !== 'production') {
if (!(deps instanceof Array) || !deps.length) {
console.warn(
'`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead.',
);
}
if (deps.every(isPrimitive)) {
console.warn(
'`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead.',
);
}
}
useCustomCompareEffect(effect, deps, isDeepEqual);
}
Example #8
Source File: useCustomCompareEffect.ts From fe-v5 with Apache License 2.0 | 6 votes |
useCustomCompareEffect = <TDeps extends DependencyList>(
effect: EffectCallback,
deps: TDeps,
depsEqual: DepsEqualFnType<TDeps>,
) => {
if (process.env.NODE_ENV !== 'production') {
if (!(deps instanceof Array) || !deps.length) {
console.warn(
'`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead.',
);
}
if (deps.every(isPrimitive)) {
console.warn(
'`useCustomCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead.',
);
}
if (typeof depsEqual !== 'function') {
console.warn(
'`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list',
);
}
}
const ref = useRef<TDeps | undefined>(undefined);
if (!ref.current || !depsEqual(deps, ref.current)) {
ref.current = deps;
}
useEffect(effect, ref.current);
}
Example #9
Source File: useDidUpdateEffect.ts From parity-bridges-ui with GNU General Public License v3.0 | 6 votes |
/**
* Exactly like React's `useEffect`, but skips initial render. Tries to
* reproduce `componentDidUpdate` behavior.
*
* @see https://stackoverflow.com/questions/53179075/with-useeffect-how-can-i-skip-applying-an-effect-upon-the-initial-render/53180013#53180013
*/
export function useDidUpdateEffect(fn: EffectCallback, inputs?: DependencyList): void {
const didMountRef = useRef(false);
return useEffect(() => {
if (didMountRef.current) fn();
else didMountRef.current = true;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, inputs);
}
Example #10
Source File: useUpdateEffect.ts From web with MIT License | 6 votes |
/**
* Effect hook that ignores the first render (not invoked on mount).
*
* @param effect Effector to run on updates
* @param deps Dependencies list, as for `useEffect` hook
*/
export function useUpdateEffect(effect: EffectCallback, deps?: DependencyList): void {
const isFirstMount = useFirstMountState();
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(isFirstMount ? noop : effect, deps);
}
Example #11
Source File: useDidMount.ts From fe-foundation with Apache License 2.0 | 5 votes |
export function useDidMount(effect: EffectCallback): void {
return useEffect(effect, []);
}
Example #12
Source File: useEffectOnce.ts From sc-translator-crx with GNU General Public License v3.0 | 5 votes |
useEffectOnce = (effect: EffectCallback) => {
// eslint-disable-next-line
useEffect(effect, []);
}
Example #13
Source File: useEffectOnce.ts From usehooks-ts with MIT License | 5 votes |
function useEffectOnce(effect: EffectCallback) {
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(effect, [])
}
Example #14
Source File: useEffectOnce.ts From tailchat with GNU General Public License v3.0 | 5 votes |
useEffectOnce = (effect: EffectCallback) => {
useEffect(effect, []);
}
Example #15
Source File: useEffectOnce.ts From fe-v5 with Apache License 2.0 | 5 votes |
useEffectOnce = (effect: EffectCallback) => {
useEffect(effect, []);
}
Example #16
Source File: dom.ts From web with MIT License | 4 votes |
describe('useConditionalEffect', () => {
it('should be defined', () => {
expect(useConditionalEffect).toBeDefined();
});
it('should render', () => {
const { result } = renderHook(() => useConditionalEffect(() => {}, undefined, []));
expect(result.error).toBeUndefined();
});
it('by default should invoke effect only in case all conditions are truthy', () => {
const spy = jest.fn();
const { rerender } = renderHook(({ cond }) => useConditionalEffect(spy, undefined, cond), {
initialProps: { cond: [1] as unknown[] },
});
expect(spy).toHaveBeenCalledTimes(1);
rerender({ cond: [0, 1, 1] });
expect(spy).toHaveBeenCalledTimes(1);
rerender({ cond: [1, {}, null] });
expect(spy).toHaveBeenCalledTimes(1);
rerender({ cond: [true, {}, [], 25] });
expect(spy).toHaveBeenCalledTimes(2);
});
it('should not be called on mount if conditions are falsy', () => {
const spy = jest.fn();
renderHook(({ cond }) => useConditionalEffect(spy, undefined, cond), {
initialProps: { cond: [null] as unknown[] },
});
expect(spy).toHaveBeenCalledTimes(0);
});
it('should invoke callback only if deps are changed and conditions match predicate', () => {
const spy = jest.fn();
const { rerender } = renderHook(({ cond, deps }) => useConditionalEffect(spy, deps, cond), {
initialProps: { cond: [false] as unknown[], deps: [1] as any[] },
});
expect(spy).toHaveBeenCalledTimes(0);
rerender({ cond: [false], deps: [2] });
expect(spy).toHaveBeenCalledTimes(0);
rerender({ cond: [true], deps: [2] });
expect(spy).toHaveBeenCalledTimes(0);
rerender({ cond: [true], deps: [3] });
expect(spy).toHaveBeenCalledTimes(1);
rerender({ cond: [true], deps: [3] });
expect(spy).toHaveBeenCalledTimes(1);
rerender({ cond: [true], deps: [4] });
expect(spy).toHaveBeenCalledTimes(2);
rerender({ cond: [false], deps: [5] });
expect(spy).toHaveBeenCalledTimes(2);
});
it('should apply custom predicate', () => {
const spy = jest.fn();
const predicateSpy = jest.fn((conditions) => truthyOrArrayPredicate(conditions));
const { rerender } = renderHook(
({ cond }) => useConditionalEffect(spy, undefined, cond, predicateSpy),
{
initialProps: { cond: [null] as unknown[] },
}
);
expect(predicateSpy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledTimes(0);
rerender({ cond: [true, {}, [], 25] });
expect(predicateSpy).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenCalledTimes(1);
rerender({ cond: [true, false, 0, null] });
expect(predicateSpy).toHaveBeenCalledTimes(3);
expect(spy).toHaveBeenCalledTimes(2);
rerender({ cond: [undefined, false, 0, null] });
expect(predicateSpy).toHaveBeenCalledTimes(4);
expect(spy).toHaveBeenCalledTimes(2);
});
it('should accept custom hooks and pass extra args to it', () => {
const callbackSpy = jest.fn();
const effectSpy = jest.fn(
(cb: EffectCallback, deps: DependencyList | undefined, _num: number) =>
useUpdateEffect(cb, deps)
);
const { rerender } = renderHook(() =>
useConditionalEffect(callbackSpy, undefined, [true], truthyAndArrayPredicate, effectSpy, 123)
);
expect(callbackSpy).not.toHaveBeenCalled();
expect(effectSpy).toHaveBeenCalledTimes(1);
expect(effectSpy.mock.calls[0][2]).toBe(123);
rerender();
expect(callbackSpy).toHaveBeenCalledTimes(1);
});
});