当前位置:网站首页>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);
边栏推荐
- 1, 基本配置
- 5. Messager framework and imessager interface
- Distributed things
- Microsoft. Bcl. Async usage summary -- in Net framework 4.5 project Net framework version 4.5 and above can use async/await asynchronous feature in C 5
- AutoUpdater. Net client custom update file
- utils session&rpc
- Express get request
- 7. know JNI and NDK
- Harmonyos actual combat - ten thousand words long article understanding service card development process
- Torch learning summary
猜你喜欢

桂林 穩健醫療收購桂林乳膠100%股權 填補乳膠產品線空白

MySQL优化

7. know JNI and NDK

Harmonyos actual combat - ten thousand words long article understanding service card development process

JVM tuning tool commands (notes)

AutoUpdater. Net client custom update file

Solution to pychart's failure in importing torch package

Dart 开发技巧

Framework program of browser self-service terminal based on IE kernel

八大排序(二)
随机推荐
Couldn't load this key (openssh ssh-2 private key (old PEM format))
Flutter theme (skin) changes
oracle跨数据库复制数据表-dblink
Redis + MySQL implements the like function
MySQL directory
MySQL index and data storage structure foundation
float
I'm late for school
Create thread pool demo
Linear-gradient()
Tclistener server and tcpclient client use -- socket listening server and socketclient use
Summary of Android knowledge points and common interview questions
Deep Learning with Pytorch- neural network
About MySQL Boolean and tinyint (1)
基于Svelte3.x桌面端UI组件库Svelte UI
9.JNI_ Necessary optimization design
训练一个图像分类器demo in PyTorch【学习笔记】
桂林 穩健醫療收購桂林乳膠100%股權 填補乳膠產品線空白
【Ubuntu-MySQL8安装与主从复制】
Use V-IF with V-for