Created
April 13, 2021 13:57
-
-
Save chaance/813dce5b0a418acc2057f7d5b88b02c2 to your computer and use it in GitHub Desktop.
Phony useState
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
// 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