A tiny React hook for temporarily flashing state. This can be useful for updating text on buttons when a user clicks. For example:
react-use-flash.mov
CodeSandbox here: https://codesandbox.io/s/modest-williamson-fk7mk?file=/src/App.tsx
npm install react-use-flash
yarn add react-use-flash
import { useFlash } from 'react-use-flash';
function App() {
const [message, flash] = useFlash('Copy', {
duration: 1000,
});
return <button onClick={() => flash('Copied!')}>{message}</button>;
}
import { useFlash } from 'react-use-flash';
function App() {
const [alert, flash] = useFlash(
{ type: 'info', message: 'Save profile' },
{
duration: 1000,
},
);
return (
<button
onClick={() =>
flash({ type: 'error', message: 'Oops! Please try again' })
}
>
{alert.message}
</button>
);
}
import { useFlash } from 'react-use-flash';
type Alert = {
type: 'info' | 'error';
message: string;
};
function App() {
const [alert, flash] = useFlash<Alert>(
{ type: 'error', message: 'Oops! Please try again' },
{
duration: 1000,
},
);
return (
<button
onClick={() =>
flash({ type: 'error', message: 'Oops! Please try again' })
}
>
{alert.message}
</button>
);
}