Skip to content

Commit

Permalink
Add isOnUnmount to onSave callback
Browse files Browse the repository at this point in the history
  • Loading branch information
jlopezxs committed Jan 31, 2024
1 parent cc25395 commit 2a8a69b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface CommonProps<TData, TReturn> {
/** The controlled form value to be auto saved */
data: TData;
/** Callback function to save your data */
onSave: (data: TData) => Promise<TReturn> | TReturn | void;
onSave: (data: TData, isOnUnmount: boolean) => Promise<TReturn> | 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 */
Expand Down
6 changes: 3 additions & 3 deletions src/useAutosave.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useEffect } from 'react';
import { useEffect, useRef } from 'react';
import { CommonProps } from './props';
import useDebounce from './useDebounce';

Expand All @@ -18,7 +18,7 @@ function useAutosave<TData, TReturn>({
if (initialRender.current) {
initialRender.current = false;
} else {
handleSave.current(debouncedValueToSave);
handleSave.current(debouncedValueToSave, false);
}
}, [debouncedValueToSave]);

Expand All @@ -33,7 +33,7 @@ function useAutosave<TData, TReturn>({
useEffect(
() => () => {
if (saveOnUnmount) {
handleSave.current(valueOnCleanup.current);
handleSave.current(valueOnCleanup.current, true);
}
},
[saveOnUnmount],
Expand Down

0 comments on commit 2a8a69b

Please sign in to comment.