mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-31 06:52:50 +08:00
✨ Flush the debounced Redux handler on unmount
Ensure that pending changes are written to the store instead of discarding them.
This commit is contained in:
parent
317ba4e8ec
commit
046e3d5699
1 changed files with 5 additions and 5 deletions
|
@ -18,20 +18,20 @@ export const useDebounceField = (
|
|||
) => {
|
||||
const [ fieldValue, setFieldValue ] = useState( storeValue );
|
||||
|
||||
// Memoize the debounced store sync
|
||||
// Memoize the debounced store sync.
|
||||
const debouncedSync = useMemo(
|
||||
() => debounce( syncToStore, delay ),
|
||||
[ syncToStore, delay ]
|
||||
);
|
||||
|
||||
// Sync field with store changes
|
||||
// Sync field with store changes.
|
||||
useEffect( () => {
|
||||
if ( storeValue !== '' && fieldValue !== storeValue ) {
|
||||
setFieldValue( storeValue );
|
||||
}
|
||||
}, [ storeValue, fieldValue ] );
|
||||
|
||||
// Handle field updates and store sync
|
||||
// Handle field updates and store sync.
|
||||
const updateField = useCallback(
|
||||
( newValue ) => {
|
||||
setFieldValue( newValue );
|
||||
|
@ -40,10 +40,10 @@ export const useDebounceField = (
|
|||
[ debouncedSync ]
|
||||
);
|
||||
|
||||
// Cleanup on unmount
|
||||
// Cleanup on unmount.
|
||||
useEffect( () => {
|
||||
return () => {
|
||||
debouncedSync.cancel();
|
||||
debouncedSync?.flush();
|
||||
};
|
||||
}, [ debouncedSync ] );
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue