Skip to content

Instantly share code, notes, and snippets.

@mahmut-gundogdu
Created June 8, 2021 19:30
Show Gist options
  • Save mahmut-gundogdu/193ad830be31807ee4e232a05aeec1d8 to your computer and use it in GitHub Desktop.
Save mahmut-gundogdu/193ad830be31807ee4e232a05aeec1d8 to your computer and use it in GitHub Desktop.
useState initial value changed when initialValue changed
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