Skip to content

Instantly share code, notes, and snippets.

@dlai0001
Created April 2, 2022 11:42
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 dlai0001/d0a20ac547dcb3d73f6015bff4509a74 to your computer and use it in GitHub Desktop.
Save dlai0001/d0a20ac547dcb3d73f6015bff4509a74 to your computer and use it in GitHub Desktop.
A crude use state implementation
// 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