Skip to content

Instantly share code, notes, and snippets.

@shuhei
Created April 24, 2017 10:14
Show Gist options
  • Save shuhei/8395b5fc96ec1ef41fbf09ac594ef56c to your computer and use it in GitHub Desktop.
Save shuhei/8395b5fc96ec1ef41fbf09ac594ef56c to your computer and use it in GitHub Desktop.
Compile JSX to string
function Foo(props) {
return (
<div className="yes">
<p>Hello, {props.name}</p>
</div>
);
}
function FooCompiled(props) {
return (
// TODO: Whitelist DOM element names
'<div class="yes">' +
// Encode values properly
'<p>' + 'Hello, ' + handleValue(props.name) + '</p>' +
'</div>'
);
}
class Something extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
{props.foos.map(foo => <Foo foo={foo} {...props.bar} />)}
</div>
);
}
}
class SomethingCompiled extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
'<div>' +
handleValue(props.foos.map(foo => ReactDOMServer.renderToStaticMarkup(
<Foo foo={foo} {...props.bar} />
))) +
'</div>'
);
}
}
// TODO: Compile third-party components or use ReactDOMServer.renderToStaticMarkup(<Foo />)
function renderCompositeComponent(type, props) {
return type instanceof React.Component ?
(new type(props)).render() :
type(props);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment