Created
April 2, 2022 11:42
-
-
Save dlai0001/d0a20ac547dcb3d73f6015bff4509a74 to your computer and use it in GitHub Desktop.
A crude use state implementation
This file contains hidden or 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 | |
const states = [] | |
let calls = -1 | |
function useState(value) { | |
const call = ++calls | |
if (states[call]) { | |
return states[call] | |
} | |
function setState(newValue) { | |
states[call][0] = newValue | |
reRender() | |
} | |
const state = [value, setState] | |
states[call] = state | |
return state | |
} | |
function reRender() { | |
calls = -1 | |
ReactDOM.render(<App />, document.getElementById('root')) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment