32 lines
716 B
JavaScript
32 lines
716 B
JavaScript
|
const stateStore = {};
|
||
|
|
||
|
let componentId;
|
||
|
let stateIndex = 0;
|
||
|
let handleStateChange;
|
||
|
|
||
|
export function useState(defaultValue) {
|
||
|
const savedId = componentId;
|
||
|
const savedIndex = stateIndex++;
|
||
|
const savedHandler = handleStateChange;
|
||
|
if(!stateStore[savedId]) {
|
||
|
stateStore[savedId] = [];
|
||
|
}
|
||
|
|
||
|
if(!stateStore[savedId][savedIndex]) {
|
||
|
stateStore[savedId][savedIndex] = [
|
||
|
defaultValue,
|
||
|
value => {
|
||
|
stateStore[savedId][savedIndex][0] = value;
|
||
|
savedHandler();
|
||
|
},
|
||
|
]
|
||
|
}
|
||
|
|
||
|
return stateStore[savedId][savedIndex]
|
||
|
}
|
||
|
export function prepareForRender(_componentId, _handleStateChange) {
|
||
|
stateIndex = 0;
|
||
|
componentId = _componentId;
|
||
|
handleStateChange = _handleStateChange;
|
||
|
}
|