当前位置:网站首页>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);
边栏推荐
- 八大排序(一)
- Numpy (constant)
- Challenge transform() 2D
- [shutter] solve failed assertion: line 5142 POS 12: '_ debugLocked‘: is not true.
- Use V-IF with V-for
- ACM intensive training graph theory exercise 3 in the summer vacation of 2020 [problem solving]
- Enum demo
- JPA naming rules
- 9.JNI_ Necessary optimization design
- Idea setting automatic package Guide
猜你喜欢
MySQL index and data storage structure foundation
Express file download
Clickhouse installation (quick start)
桂林 稳健医疗收购桂林乳胶100%股权 填补乳胶产品线空白
Flutter 0001, environment configuration
Small program learning path 1 - getting to know small programs
布隆过滤器
Dart asynchronous task
Notes on masking and padding in tensorflow keras
Framework program of browser self-service terminal based on IE kernel
随机推荐
Express file download
Solution to the eighth training competition of 2020 Provincial Games
Dart asynchronous task
How do I start? (continuously updating)
Bluetooth BT RF test (forwarding)
Express - static resource request
Xlnet (generalized autorefressive trainingfor language understanding) paper notes
[wechat applet] realize applet pull-down refresh and pull-up loading
仿照微信Oauth2.0接入方案
(zero) most complete JVM knowledge points
Deberta (decoding enhanced Bert with distinguished attention)
JVM tuning tool commands (notes)
Distributed things
ES6 learning path (III) deconstruction assignment
Framework program of browser self-service terminal based on IE kernel
Generate directory in markdown
JVM memory common parameter configuration set
ABAP-时间函数
ReturnJson,让返回数据多一些自定义数据或类名
4. use ibinder interface flexibly for short-range communication