Skip to content

Instantly share code, notes, and snippets.

@rickklaasboer
Created January 18, 2023 11:07
Show Gist options
  • Save rickklaasboer/1b2e66fe9fdfcf655da5d4b70a88921f to your computer and use it in GitHub Desktop.
Save rickklaasboer/1b2e66fe9fdfcf655da5d4b70a88921f to your computer and use it in GitHub Desktop.
Legacy state hook implementation (didn't test this 100%, should work though)
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