当前位置:网站首页>11. customize hooks
11. customize hooks
2022-06-30 09:40:00 【Shanchuan Pro】
- Only when the component is initialized Hook.
import {
useEffect } from 'react';
import {
isFunction } from '@src/utils/type';
export default function useMount(func) {
if (process.env.NODE_ENV === 'development') {
if (!isFunction(func)) {
console.error(`useMount: parameter \`fn\` expected to be a function, but got "${
typeof func}".`);
}
}
useEffect(() => {
func?.();
}, []);
}
- Returns the current latest value Hook, Closure problems can be avoided .
import {
useRef } from 'react';
function useLatest(value) {
const ref = useRef(value);
ref.current = value;
return ref;
}
export default useLatest;
- throttle hook
import {
throttle } from 'lodash';
import {
useMemo } from 'react';
import {
isFunction } from '@src/utils/type';
import useLatest from '../useLatest';
import useUnmount from '../useUnMount';
function useThrottleFn(fn, options) {
if (process.env.NODE_ENV === 'development') {
if (!isFunction(fn)) {
console.error(`useThrottleFn expected parameter is a function, got ${
typeof fn}`);
}
}
const fnRef = useLatest(fn);
const wait = options?.wait ?? 1000;
const debounced = useMemo(
() =>
throttle(
(...args) => {
fnRef.current(...args);
},
wait,
options,
),
[],
);
useUnmount(() => {
debounced.cancel();
});
return {
run: debounced,
cancel: debounced.cancel,
flush: debounced.flush,
};
}
export default useThrottleFn;
- Shake proof hook
import {
debounce } from 'lodash';
import {
useMemo } from 'react';
import {
isFunction } from '@src/utils';
import useLatest from '../useLatest';
import useUnmount from '../useUnMount';
/** * @description: Used to handle anti shake hook * @param {Function} fn: Functions that require anti shake execution * @param {Object} options: Configure anti shake behavior * wait: Waiting time * maxWait: Maximum waiting time * @return {Object} * run: Trigger execution fn, Function arguments will be passed to fn * cancel: Remove Anti shake * flush: Immediately call the current anti shake function */
function useDebounceFn(fn, options) {
if (process.env.NODE_ENV === 'development') {
if (!isFunction(fn)) {
console.error(`useDebounceFn expected parameter is a function, got ${
typeof fn}`);
}
}
const fnRef = useLatest(fn);
const wait = options?.wait ?? 1000;
const debounced = useMemo(
() =>
debounce(
(...args) => {
fnRef.current(...args);
},
wait,
options,
),
[],
);
useUnmount(() => {
debounced.cancel();
});
return {
run: debounced,
cancel: debounced.cancel,
flush: debounced.flush,
};
}
export default useDebounceFn;
- It can be done to effect Array dependent for deep comparison hook
import {
useEffect, useRef } from 'react';
import {
isEqual } from 'lodash';
const depsEqual = (aDeps, bDeps) => isEqual(aDeps, bDeps);
function createDeepCompareEffect(hook) {
return (effect, deps) => {
const ref = useRef();
const signalRef = useRef(0);
if (deps === undefined || !depsEqual(deps, ref.current)) {
ref.current = deps;
signalRef.current += 1;
}
hook(effect, [signalRef.current]);
};
}
export default createDeepCompareEffect(useEffect);
边栏推荐
- Get to know handler again
- Cb/s Architecture - Implementation Based on cef3+mfc
- Mysq database remote connection error, remote connection is not allowed
- qmlplugindump executable not found.It is required to generate the qmltypes file for VTK Qml
- Tutorial for beginners of small programs day01
- 2021-10-20
- Agp7.0|kts makes a reinforced plug-in
- Create thread pool demo
- Deep Learning with Pytorch- neural network
- 目标检测yolov5开源项目调试
猜你喜欢

4. use ibinder interface flexibly for short-range communication

Flutter 0001, environment configuration

Express - static resource request

Terminal -- Zsh of terminal three swordsmen

Express get request

Mysq database remote connection error, remote connection is not allowed

机器学习笔记 九:预测模型优化(防止欠拟合和过拟合问题发生)

云技能提升好伙伴,亚马逊云师兄今天正式营业

目标检测yolov5开源项目调试

Notes on masking and padding in tensorflow keras
随机推荐
Golang magic code
2021-10-20
Splice and slice functions of JS
抽象类和接口
I once met a girl whom I most wanted to take care of all my life. Later... No later
Flutter的特别之处在哪里
9.JNI_ Necessary optimization design
Tablet PC based ink handwriting recognition input method
Clickhouse installation (quick start)
Use V-IF with V-for
CentOS MySQL installation details
Configuring MySQL for error reporting
Simple redis lock
Notes on masking and padding in tensorflow keras
Cb/s Architecture - Implementation Based on cef3+mfc
prometheus 监控之 ntp_exporter
Numpy (constant)
Redis docker 主从模式与哨兵sentinel
银河麒麟server-V10配置镜像源
JVM tuning tool commands (notes)