Skip to content

Instantly share code, notes, and snippets.

@pshaddel
Last active April 13, 2024 17:14
Show Gist options
  • Save pshaddel/070895252abb1e2d2b48e03ad47c5011 to your computer and use it in GitHub Desktop.
Save pshaddel/070895252abb1e2d2b48e03ad47c5011 to your computer and use it in GitHub Desktop.
Use State Implementation in Javascript
const React = (function () {
let _state;
return {
useState: function (initialValue) {
if (typeof _state === 'undefined') _state = initialValue;
function setState(newVal) {
_state = newVal;
}
return [_state, setState];
},
render: function (Component) {
const C = Component();
C.render();
return C;
}
};
})();
const Component = function () {
const [count, setCount] = React.useState(0);
return {
render: function () {
console.log(count);
},
click: function () {
setCount(count + 1);
}
};
}
var App = React.render(Component); // 0
App.click();
var App = React.render(Component); // 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment