React Hello World Example with ES6+JSX
A basic Hello World widget with in browser JSX transformed + ES6 support
by yan kang
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/JSXTransformer.js"></script>
<script src="https://facebook.github.io/react/js/jsfiddle-integration.js"></script>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
CSS
.widget {
width: 202px;
margin:100px auto;
// border:1px solid black;
padding:20px;
}
.name {
color: red;
}
.default-label {
border: 1px solid black;
padding: 10px;
margin-bottom: 10px;
width:180px;
}
.green-label {
border: 1px solid green;
padding: 10px;
margin-bottom: 10px;
width: 180px;
}
.default-input {
border: 1px solid #eaeaea;
padding: 10px;
width:180px;
}
.important-input {
border: 2px solid red;
width:180px;
}
img.center {
width: 100px;
height: 100px;
display: block;
margin: 15px auto;
}
JavaScript 1.7
/// tiny react lib start
function createElement(comp, props, ...args) {
let children = []
for(let i = 0; i< args.length;i++){
if(args[i] instanceof Array) {
children = children.concat(args[i])
} else {
children.push(args[i])
}
}
return {
nodeName: comp,
props: props || {},
children
}
}
function render(vnode, parent) {
let dom
if(typeof vnode == "string") {
dom = document.createTextNode(vnode)
parent.appendChild(dom)
} else if(typeof vnode.nodeName == "string") {
dom = document.createElement(vnode.nodeName)
setAttrs(dom, vnode.props)
parent.appendChild(dom)
for(let i = 0; i < vnode.children.length; i++) {
render(vnode.children[i], dom)
}
} else if (typeof vnode.nodeName == "function") {
let func = vnode.nodeName
let inst = new func(vnode.props)
let innerVnode = inst.render()
render(innerVnode, parent)
}
}
function setAttrs(dom, props) {
const allKeys = Object.keys(props)
allKeys.forEach(k => {
const v = props[k]
if(k == "className") {
dom.setAttribute("class", v)
return
}
if(k == "style") {
if(typeof v == "string") {
dom.style.cssText = v
}
if(typeof v == "object") {
for (let i in v) {
dom.style[i] = v[i]
}
}
return
}
if(k[0] == "o" && k[1] == "n") {
const capture = (k.indexOf("Capture") != -1)
dom.addEventListener(k.substring(2).toLowerCase(), capture)
return
}
dom.setAttribute(k, v)
})
}
React = {}
React.createElement = createElement
class Component {}
React.Component = Component
/// tiny react lib end
class HelloWorld extends Component {
render() {
return <div style={{...