Skip to content

Instantly share code, notes, and snippets.

@GalindoSVQ
Created November 2, 2023 14:46
Show Gist options
  • Save GalindoSVQ/4d367b2855fee91250dae298657a6113 to your computer and use it in GitHub Desktop.
Save GalindoSVQ/4d367b2855fee91250dae298657a6113 to your computer and use it in GitHub Desktop.
import * as React from 'react';
export default function usePrevious(newValue: string) {
const [value, setValue] = React.useState<string>(newValue);
const [preValue, setPreValue] = React.useState<string | null>(null);
if (newValue !== value) {
setPreValue(value);
setValue(newValue);
}
return preValue;
}
@GalindoSVQ
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment