From d2200d38fe60c5c645291f2cfbb74926313f20b2 Mon Sep 17 00:00:00 2001 From: Samhir Tarif <89391183+samhirtarif@users.noreply.github.com> Date: Wed, 24 May 2023 21:46:27 +0500 Subject: [PATCH] Add useCallback on hook methods (#48) --- package-lock.json | 4 ++-- package.json | 2 +- src/hooks/useAudioRecorder.ts | 31 ++++++++++++++++++++++--------- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 22754588..afb5566d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "react-audio-voice-recorder", - "version": "1.1.6", + "version": "1.1.7", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "react-audio-voice-recorder", - "version": "1.1.6", + "version": "1.1.7", "license": "MIT", "dependencies": { "react": "^18.2.0", diff --git a/package.json b/package.json index 123d4373..e794a5a4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "react-audio-voice-recorder", "private": false, - "version": "1.1.6", + "version": "1.1.7", "license": "MIT", "author": "", "repository": { diff --git a/src/hooks/useAudioRecorder.ts b/src/hooks/useAudioRecorder.ts index 0b6f9647..d1680370 100644 --- a/src/hooks/useAudioRecorder.ts +++ b/src/hooks/useAudioRecorder.ts @@ -49,17 +49,17 @@ const useAudioRecorder: ( const [timerInterval, setTimerInterval] = useState(); const [recordingBlob, setRecordingBlob] = useState(); - const _startTimer: () => void = () => { + const _startTimer: () => void = useCallback(() => { const interval = setInterval(() => { setRecordingTime((time) => time + 1); }, 1000); setTimerInterval(interval); - }; + }, [setRecordingTime, setTimerInterval]); - const _stopTimer: () => void = () => { + const _stopTimer: () => void = useCallback(() => { timerInterval != null && clearInterval(timerInterval); setTimerInterval(undefined); - }; + }, [timerInterval, setTimerInterval]); /** * Calling this method would result in the recording to start. Sets `isRecording` to true @@ -86,23 +86,36 @@ const useAudioRecorder: ( console.log(err.name, err.message, err.cause); onNotAllowedOrFound?.(err); }); - }, [timerInterval]); + }, [ + timerInterval, + setIsRecording, + setMediaRecorder, + _startTimer, + setRecordingBlob, + onNotAllowedOrFound, + ]); /** * Calling this method results in a recording in progress being stopped and the resulting audio being present in `recordingBlob`. Sets `isRecording` to false */ - const stopRecording: () => void = () => { + const stopRecording: () => void = useCallback(() => { mediaRecorder?.stop(); _stopTimer(); setRecordingTime(0); setIsRecording(false); setIsPaused(false); - }; + }, [ + mediaRecorder, + setRecordingTime, + setIsRecording, + setIsPaused, + _stopTimer, + ]); /** * Calling this method would pause the recording if it is currently running or resume if it is paused. Toggles the value `isPaused` */ - const togglePauseResume: () => void = () => { + const togglePauseResume: () => void = useCallback(() => { if (isPaused) { setIsPaused(false); mediaRecorder?.resume(); @@ -112,7 +125,7 @@ const useAudioRecorder: ( _stopTimer(); mediaRecorder?.pause(); } - }; + }, [mediaRecorder, setIsPaused, _startTimer, _stopTimer]); return { startRecording,