Skip to content

Instantly share code, notes, and snippets.

@andrewagain
Created July 19, 2015 04:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewagain/3589bdbb51906c182a84 to your computer and use it in GitHub Desktop.
Save andrewagain/3589bdbb51906c182a84 to your computer and use it in GitHub Desktop.
JSX Example
/** @jsx createNode */
function createNode(tagName, attrs, ...children) {
const node = attrs || {};
node.name = tagName;
if (children && children.length > 0) {
node.children = children;
}
return node;
}
const layout = (
<widget>
<row>
<elem key='#1'/>
<elem key='#2'/>
</row>
<elem key='#3'/>
</widget>
);
console.log(JSON.stringify(layout, null, 2));
/*
Output:
{
"name": "widget",
"children": [
{
"name": "row",
"children": [
{
"key": "#1",
"name": "elem"
},
{
"key": "#2",
"name": "elem"
}
]
},
{
"key": "#3",
"name": "elem"
}
]
}
*/
@andrewagain
Copy link
Author

Run this with babel-node jsx-example.js. No other modules or dependencies are necessary because babel supports JSX.

@andrewagain
Copy link
Author

FYI this comment informs babble what method to use for converting JSX into objects: /** @jsx createNode */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment