Skip to content

Instantly share code, notes, and snippets.

@artygrand
Created October 6, 2021 05:30
Show Gist options
  • Save artygrand/ab836128f241b7886740638be7a82e90 to your computer and use it in GitHub Desktop.
Save artygrand/ab836128f241b7886740638be7a82e90 to your computer and use it in GitHub Desktop.
Wrap react's setState with callback or mutator
const wrappedSetInvoice = useCallback((value: ((i: InvoiceModel) => InvoiceModel) | InvoiceModel) => {
setInvoice(old => {
const updated = value instanceof Function ? value(old) : value;
return {...updated, dirty: true};
});
}, [setInvoice]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment