Skip to content

Instantly share code, notes, and snippets.

@jomurgel
Last active January 7, 2021 16:20
Show Gist options
  • Save jomurgel/4c56ce1492b0247ade013c7fcf2550b1 to your computer and use it in GitHub Desktop.
Save jomurgel/4c56ce1492b0247ade013c7fcf2550b1 to your computer and use it in GitHub Desktop.
useStateWithCallback Custom Hook — Fire callback function after state change.
import { useState, useEffect } from 'react';
/**
* Fire callback function after state change.
*
* @param {any} initialState intial state value.
* @param {function} callback callback after state change.
*/
const useStateWithCallback = (initialState, callback) => {
const [state, setState] = useState(initialState);
useEffect(() => callback(state), [state, callback]);
return [state, setState];
};
export default useStateWithCallback;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment