From 2a8a69bfdf76ac185bbdbb91ef743b49a9f367ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jordi=20L=C3=B3pez?= Date: Wed, 31 Jan 2024 13:06:13 +0100 Subject: [PATCH] Add isOnUnmount to onSave callback --- src/props.ts | 2 +- src/useAutosave.tsx | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/props.ts b/src/props.ts index 818cad5..9495a55 100644 --- a/src/props.ts +++ b/src/props.ts @@ -4,7 +4,7 @@ export interface CommonProps { /** The controlled form value to be auto saved */ data: TData; /** Callback function to save your data */ - onSave: (data: TData) => Promise | TReturn | void; + onSave: (data: TData, isOnUnmount: boolean) => Promise | TReturn | void; /** The number of milliseconds between save attempts. Defaults to 2000 */ interval?: number; /** Set to false if you do not want the save function to fire on unmount */ diff --git a/src/useAutosave.tsx b/src/useAutosave.tsx index bbc082d..281a35b 100644 --- a/src/useAutosave.tsx +++ b/src/useAutosave.tsx @@ -1,4 +1,4 @@ -import { useRef, useEffect } from 'react'; +import { useEffect, useRef } from 'react'; import { CommonProps } from './props'; import useDebounce from './useDebounce'; @@ -18,7 +18,7 @@ function useAutosave({ if (initialRender.current) { initialRender.current = false; } else { - handleSave.current(debouncedValueToSave); + handleSave.current(debouncedValueToSave, false); } }, [debouncedValueToSave]); @@ -33,7 +33,7 @@ function useAutosave({ useEffect( () => () => { if (saveOnUnmount) { - handleSave.current(valueOnCleanup.current); + handleSave.current(valueOnCleanup.current, true); } }, [saveOnUnmount],