Created
September 30, 2019 18:13
-
-
Save mikaelbr/b58116500717aa911dad338303488077 to your computer and use it in GitHub Desktop.
Code example from blogpost https://medium.com/variant-as/abstracting-the-dom-by-making-ad-hoc-react-like-library-1c9b42618e63
This file contains 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
const { button } = createUiFramework(); | |
let initialState = { counter: 0 }; | |
// Our application taking a state. | |
function app(state) { | |
// When we click on the button, rerender with new state where the counter is incremented | |
const onClick = () => render(app, { ...state, counter: state.counter + 1 }); | |
return button({ type: "button", onClick }, `Increment ${state.counter}!`); | |
} | |
// Render function similar to React. | |
function render(fn, s) { | |
// Empty body, just to illustrate a point. Shouldn't be used in production. | |
document.body.innerHTML = ""; | |
// Re-add our components with state. | |
document.body.appendChild(fn(s)); | |
} | |
// Do initial render | |
render(app, initialState); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment