Skip to content

Instantly share code, notes, and snippets.

@chaance
Created April 13, 2021 13:57
Show Gist options
  • Save chaance/813dce5b0a418acc2057f7d5b88b02c2 to your computer and use it in GitHub Desktop.
Save chaance/813dce5b0a418acc2057f7d5b88b02c2 to your computer and use it in GitHub Desktop.
Phony useState
// For Reference
import * as React from 'react'
import * as ReactDOM from 'react-dom'
const states: any[] = []
let calls = -1
function useState<S>(value: S) {
const call = ++calls
if (states[call]) {
return states[call]
}
function setState(newValue: S) {
states[call][0] = newValue
reRender()
}
const state = [value, setState]
states[call] = state
return state
}
function reRender() {
calls = -1
ReactDOM.render(<Component />, document.getElementById('root'))
}
reRender();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment