Skip to content

Instantly share code, notes, and snippets.

@vojtatranta
Last active April 3, 2016 18:02
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 vojtatranta/281eb9d3face7e074b4d6c7b101a3290 to your computer and use it in GitHub Desktop.
Save vojtatranta/281eb9d3face7e074b4d6c7b101a3290 to your computer and use it in GitHub Desktop.
const passProps = (props, component) => {
return {
type: 'LOCAL_STATE',
props,
component
}
}
const getElementWithLocalState = ({ clicked = false }) => {
return passProps(props, (update, props) => {
const onClick = () => {
update({ clicked: true })
}
return document.createTextNode(props.clicked ? 'clicked!' : 'not clicked')
})
}
//API:
// When this function is called first time, "renderer" should see that it does not return DOM and than it should take returned function
// and call it with update argument, which is a function that gets new props as argument
//renderer.js
const replaceElement = (oldEl, newEl) => {
oldEl.parentNode.replaceChild(newEl, oldEl)
}
const createElementUpdate = (rootEl, path, componentFn) => {
return (state) => {
let element = getElementByPath(path, rootEl)
let updateFn = createElementUpdate(rootEl, path, componentFn)
replaceElement(element, componentFn(updateFn, state))
}
}
let maybeDom = getElementWithLocalState({})
if (maybeDom['type'] == 'LOCAL_STATE') {
let updateFn = createElementUpdate(document.body, [ 0 ], maybeDom.component)
dom = maybeDom.component(updateFn, maybeDom['props'])
} else {
let dom = maybeDom
}
document.body.appendChild(dom)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment