Created
April 24, 2017 10:14
-
-
Save shuhei/8395b5fc96ec1ef41fbf09ac594ef56c to your computer and use it in GitHub Desktop.
Compile JSX to string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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