React hooks 怎样做防抖?

参考文章:
https://blog.csdn.net/wangweiscsdn/article/details/107844516
https://blog.csdn.net/weixin_47958188/article/details/107231598

1. lodash防抖

需要下载loadsh依赖:

# yarn add lodash
npm i lodash

引入:

// lodash 支持按需加载,有利于打包结果优化
import debounce from 'lodash/debounce';
// 不建议下面这样使用,因为这样会加载整个模块。
import _ from 'lodash'
_.debounce()

核心代码:
  /**
   * @description: 防抖
   * @param {*} debounce
   * @return {*}
   */
  const debounceSubmit = debounce(() => {
    form.submit();
  }, 300);
完整代码:
import React, { useState } from 'react';
import { Row, Col, Form, Input, } from 'antd';
import debounce from 'lodash/debounce';


type ITestFilterProps = {
  store: any;
};

const TestFilter: React.FC<ITestFilterProps> = ({ store }) => {
  const [form] = Form.useForm();

  const handleFilterFinish = (values: any) => {
    console.log(values, 'values----')
    //接口
  };

  /**
   * @description: 防抖
   * @param {*} debounce
   * @return {*}
   */
  const debounceSubmit = debounce(() => {
    form.submit();
  }, 300);

  return (
  	 <Row>
    	<Col span= { 18} >
		    <Form layout="inline" form = { form } onFinish = { handleFilterFinish } >
		      <Form.Item name="name" >
		        <Input
		              placeholder="名称搜索"
					  onChange = { debounceSubmit }
					  allowClear = { true}
			    />
			    </Form.Item>
		    < /Form>
	    < /Col>
    < /Row>
  );
};

export default TestFilter;

2. React hooks自定义防抖

核心代码:
// 防抖函数
  function useDebounce(fn, delay) {
    const { current } = useRef({ fn, timer: null });
    useEffect(function () {
      current.fn = fn;
    }, [fn]);

    return useCallback(function f(...args) {
      if (current.timer) {
        clearTimeout(current.timer);
      }
      current.timer = setTimeout(() => {
        current.fn.call(this, ...args);
      }, delay);
    })
  }
  const handleValue = useDebounce(v => { // 获取input的值
    const value = v.target.value
    console.log({ value })
    form.submit();
  }, 800)
完整代码:
import React, { useEffect, useCallback, useRef } from 'react';
import { Row, Col, Form, Input } from 'antd';
import debounce from 'lodash/debounce';

type ITestFilterProps = {
  store: any;
};

const TestFilter: React.FC<ITestFilterProps> = ({ store }) => {
  const [form] = Form.useForm();

  const handleFilterFinish = (values: any) => {
    console.log(values, 'values----')
    //接口
  };

  // 防抖函数
  function useDebounce(fn, delay) {
    const { current } = useRef({ fn, timer: null });
    useEffect(function () {
      current.fn = fn;
    }, [fn]);

    return useCallback(function f(...args) {
      if (current.timer) {
        clearTimeout(current.timer);
      }
      current.timer = setTimeout(() => {
        current.fn.call(this, ...args);
      }, delay);
    })
  }
  const handleValue = useDebounce(v => { // 获取input的值
    const value = v.target.value
    console.log({ value })
    form.submit();
  }, 800)


  return (
    <Row>
	    <Col span= { 18} >
		    <Form layout="inline" form = { form } onFinish = { handleFilterFinish } >
		      <Form.Item name="name" >
		        <Input
		              placeholder="名称搜索"
					  allowClear = { true}
					  onChange = { v => handleValue(v)}
					/>
			  < /Form.Item>
		  < /Form>
	  < /Col>
  < /Row>
  );
};

export default TestFilter;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值