React實(shí)現(xiàn)表單提交防抖功能的示例代碼
概述
在 React 應(yīng)用中,防抖(Debounce)技術(shù)可以有效地限制函數(shù)在短時(shí)間內(nèi)的頻繁調(diào)用,特別適用于表單提交場(chǎng)景。本文將詳細(xì)介紹如何利用 Lodash 庫(kù)中的 debounce
函數(shù),在 React 表單提交中實(shí)現(xiàn)防抖功能。
防抖原理
防抖技術(shù)的核心思想是:在指定的延遲時(shí)間內(nèi),如果再次觸發(fā)事件,則重新開(kāi)始計(jì)時(shí)。只有當(dāng)延遲時(shí)間完全結(jié)束后,才會(huì)執(zhí)行目標(biāo)函數(shù)。這樣可以防止函數(shù)因?yàn)轭l繁的觸發(fā)而導(dǎo)致的不必要的計(jì)算或者操作。
實(shí)現(xiàn)步驟
- 引入依賴: 首先,在 React 組件中引入 Lodash 庫(kù)和所需的 React 鉤子。
- 創(chuàng)建引用變量: 使用
useRef
創(chuàng)建一個(gè)引用變量hasClicked
,用于追蹤按鈕的點(diǎn)擊狀態(tài)。 - 定義操作函數(shù): 定義一個(gè)
doSomething
函數(shù),這里可以放置表單提交的邏輯或其他需要執(zhí)行的操作。 - 應(yīng)用 Debounce: 利用
useCallback
鉤子和 Lodash 的_.debounce
方法,創(chuàng)建一個(gè)防抖函數(shù)debouncedFunction
。這個(gè)函數(shù)負(fù)責(zé)重置hasClicked
狀態(tài)。 - 處理點(diǎn)擊事件: 在
handleClick
函數(shù)中,通過(guò)檢查hasClicked
的狀態(tài)來(lái)決定是否執(zhí)行操作。如果按鈕未被點(diǎn)擊過(guò),則執(zhí)行doSomething
,并通過(guò)調(diào)用debouncedFunction
啟動(dòng)防抖計(jì)時(shí)器。如果按鈕已被點(diǎn)擊,顯示提示信息。 - 渲染組件: 在組件中渲染一個(gè)按鈕,并將
handleClick
函數(shù)綁定到其點(diǎn)擊事件。
完整代碼示例
import React, { useCallback, useRef } from 'react'; import _ from 'lodash'; import { message } from 'antd'; const MyComponent = () => { const hasClicked = useRef(false); const doSomething = () => { console.log('執(zhí)行操作'); // 這里放置表單提交或其他操作的邏輯 }; const debouncedFunction = useCallback(_.debounce(() => { hasClicked.current = false; }, 1000), []); const handleClick = () => { if (!hasClicked.current) { doSomething(); hasClicked.current = true; debouncedFunction(); } else { message.info('請(qǐng)稍后再試'); } }; return ( <button onClick={handleClick}> 點(diǎn)擊我 </button> ); }; export default MyComponent;
English Version
Implementing Debounce in React Form Submissions Using Lodash
Overview
In React applications, debounce is a technique to limit frequent function calls over a short period, particularly useful in form submission scenarios. This article details how to use the debounce
function from the Lodash library to implement debounce in React form submissions.
Principle of Debounce
The core idea of debounce is: if the event is triggered again within a specified delay period, the timer resets. The target function is executed only after the delay period has fully elapsed. This prevents unnecessary computations or operations due to frequent triggering of the function.
Implementation Steps
Import Dependencies: First, import the Lodash library and necessary React hooks in your React component.
Create Reference Variable: Use useRef
to create a reference variable hasClicked
to track the button's click status.
Define Action Function: Define a doSomething
function where you can place the logic for form submission or other actions to be executed.
Apply Debounce: Create a debounced function debouncedFunction
using the useCallback
hook and Lodash's _.debounce
method. This function is responsible for resetting the hasClicked
state.
Handle Click Event: In the handleClick
function, decide whether to perform the action based on the state of hasClicked
. If the button has not
been clicked, execute doSomething
and start the debounce timer by calling debouncedFunction
. If the button has already been clicked, display a message.
Render Component: Render a button in the component and bind the handleClick
function to its click event.
到此這篇關(guān)于React實(shí)現(xiàn)表單提交防抖功能的示例代碼的文章就介紹到這了,更多相關(guān)React防抖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解React?如何防止?XSS?攻擊論$$typeof?的作用
這篇文章主要介紹了詳解React?如何防止?XSS?攻擊論$$typeof?的作用,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07React使用UI(Ant?Design)框架的詳細(xì)過(guò)程
Ant?Design主要用于中后臺(tái)系統(tǒng)的使用,它提供了豐富的組件和工具,可以幫助開(kāi)發(fā)人員快速構(gòu)建出美觀、易用的界面,同時(shí),Ant?Design還提供了詳細(xì)的文檔和示例,方便開(kāi)發(fā)者學(xué)習(xí)和使用,這篇文章主要介紹了React使用UI(Ant?Design)框架,需要的朋友可以參考下2023-12-12React Native如何消除啟動(dòng)時(shí)白屏的方法
本篇文章主要介紹了React Native如何消除啟動(dòng)時(shí)白屏的方法,詳細(xì)的介紹了解決的方法,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08React通過(guò)父組件傳遞類名給子組件的實(shí)現(xiàn)方法
React 是一個(gè)用于構(gòu)建用戶界面的 JAVASCRIPT 庫(kù)。這篇文章主要介紹了React通過(guò)父組件傳遞類名給子組件的方法,需要的朋友可以參考下2017-11-11react?hooks深拷貝后無(wú)法保留視圖狀態(tài)解決方法
這篇文章主要為大家介紹了react?hooks深拷貝后無(wú)法保留視圖狀態(tài)解決示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06TypeScript在React中的應(yīng)用技術(shù)實(shí)例解析
這篇文章主要為大家介紹了TypeScript在React中的應(yīng)用技術(shù)實(shí)例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04React配置多個(gè)代理實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求返回問(wèn)題
這篇文章主要介紹了React之配置多個(gè)代理實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求返回問(wèn)題,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08react中antd Upload手動(dòng)上傳的示例
本文主要介紹了react中antd Upload手動(dòng)上傳的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04