Created
June 8, 2021 19:30
-
-
Save mahmut-gundogdu/193ad830be31807ee4e232a05aeec1d8 to your computer and use it in GitHub Desktop.
useState initial value changed when initialValue changed
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 {useEffect, useState} from 'react'; | |
export function useStateWithDep(defaultValue: any) { | |
const [value, setValue] = useState(defaultValue); | |
useEffect(() => { | |
setValue(defaultValue); | |
}, [defaultValue]); | |
return [value, setValue]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment