Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onSave: (data: TData, isOnUnmount: boolean) => Promise<TReturn> | TReturn | void;
onSave: (data: TData, meta: {unmounting: boolean}) => Promise<TReturn> | TReturn | void;

Could we wrap this in an object so if we ever need to add more metadata it is not a breaking change to the callback's contract?

/** 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