Skip to content

Instantly share code, notes, and snippets.

@kalysr
Created April 24, 2020 18:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalysr/460596a1e77c0ae7100bc2f3a3385314 to your computer and use it in GitHub Desktop.
Save kalysr/460596a1e77c0ae7100bc2f3a3385314 to your computer and use it in GitHub Desktop.
let index = 0;
let defaultState = [];
const useState = (state) => {
index++;
if (defaultState[index] === undefined) {
defaultState[index] = state;
}
const setState = (newState) => {
defaultState[index] = newState;
};
return [defaultState[index], setState];
};
function Component1() {
const [inputValue, setInputValue] = useState(0);
setInputValue(inputValue + 1);
console.log("inputValue:", inputValue);
}
function Component2() {
const [inputValue, setInputValue] = useState(0);
setInputValue(inputValue + 1);
console.log("inputValue:", inputValue);
}
function Component3() {
const [inputValue, setInputValue] = useState(0);
setInputValue(inputValue + 1);
console.log("inputValue:", inputValue);
}
function render(){
Component1();
Component2();
Component3();
index = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment