Created
January 18, 2023 11:07
-
-
Save rickklaasboer/1b2e66fe9fdfcf655da5d4b70a88921f to your computer and use it in GitHub Desktop.
Legacy state hook implementation (didn't test this 100%, should work though)
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} from 'react' | |
// Example usage: | |
// | |
// const [state, setState] = useLegacyState<State>({ | |
// history: [], | |
// queue: [], | |
// song: null, | |
// songTime: null, | |
// paused: true, | |
// }) | |
export default function useLegacyState<T>( | |
initialValue: T | |
): [T, (setter: Partial<T> | ((state: T) => T)) => void] { | |
const [state, _setState] = useState<T>(initialValue) | |
function setState(setter: ((state: T) => T) | Partial<T>) { | |
if (setter instanceof Function) { | |
_setState(setter(state)) | |
} else { | |
_setState({...state, ...setter}) | |
} | |
} | |
return [state, setState] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment