Last active
January 7, 2021 16:20
-
-
Save jomurgel/4c56ce1492b0247ade013c7fcf2550b1 to your computer and use it in GitHub Desktop.
useStateWithCallback Custom Hook — Fire callback function after state change.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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