Skip to content

Instantly share code, notes, and snippets.

@zbyte64
Forked from anonymous/sample.jsx
Last active August 29, 2015 14:20
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 zbyte64/f415cffefc1e271de66b to your computer and use it in GitHub Desktop.
Save zbyte64/f415cffefc1e271de66b to your computer and use it in GitHub Desktop.
Custom React
/** @jsx this */
//classic callback
function clicker(props, children, render) {
var clicks = 0;
function clicked(event) {
event.preventDefault();
clicks += 1;
render(); //or render(clicks) to allow caching
}
return function() {
return <p>
<Gteeting format="large"/>
<button onClick={clicked}>{clicks}</button>
</p>
}
}
function Greeting(props) {
if (props.format === "large") {
return <h2>Hello</h2>
}
return <p>Hello</p>
}
new Mount(document.body, clicker);
class Mount {
constructor(element, render, props, children) {
//store dynamic nodes here for render updates
var this.renderTable = {};
var rootVDOM = this.createElement(render, props, children);
this.mergeWithDOM(rootVDOM, element);
}
mergeWithDOM(virtualDOM, toElement) {
}
//createElement is namespaced unlike react
createElement(tagName, props, children) {
if (_.isFunction(tagName)) {
var key = props.id || uuid.v4();
var result = tagName.call(this, props, children, this.scheduleRender.bind(this, key));
if _.isFunction(result) {
this.renderTable[key] = {
render: result,
}
}
} else if (_.isString(tagName) {
return this.tagFuntions[tagName].call(this);
}
}
scheduleRender(element, key, state) {
//if you want performance, use immutables!
if (this.renderTable[key].lastState !== state) {
this.renderTable[key].lastState = state;
var vDOM = this.renderTable[key].render.call(this);
this.mergeWithDOM(vDOM, element);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment