Skip to content

Instantly share code, notes, and snippets.

@midudev
Created June 17, 2019 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save midudev/5ec4ff91df28bd75c2f7b6bbd7f7fc06 to your computer and use it in GitHub Desktop.
Save midudev/5ec4ff91df28bd75c2f7b6bbd7f7fc06 to your computer and use it in GitHub Desktop.
quick'n'dirty state replacement that emulates the general behaviour of the old setState
// hook to emulate legacy state behaviour
const useLegacyState = (initialState = {}) => {
const [state, setState] = useState(initialState)
function setLegacyState (partialNewState) {
let newState = {...state}
if (typeof partialNewState === 'object' && partialNewState !== null) {
newState = {...newState, ...partialNewState}
}
setState(newState)
}
return [state, setLegacyState]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment