In React JSX it does not appear to be possible to do something like this:
render: function() { return ( <{this.props.component.slug} className='text'> {this.props.component.value} </{this.props.component.slug}> ); }
I get a parse error: Unexpected token {. Is this not something React can handle?
I'm designing this component so that under the hood, the values stored in this.props.component.slug
will contain valid HTML elements (h1, p, etc.). Is there any way to make this work?
Dynamic component rendering with React To allow us to render a JSON by dynamic components using their name we first have to loop through the array of components itself. Below you can see that we're using the map function to do exactly that.
As the building blocks of React applications, components are dynamic, in that they can describe a template of HTML and fill in variable data. This lesson builds a real example of a blogging application to illustrate dynamic components.
To render the components based on the component key in the JSON config, we first need to create an object that maps the components with the component key. As you can see, I have mapped the component key with the corresponding Reactstrap component. At the top, we will import all the required components.
You should not put component slug in curly braces:
var Hello = React.createClass({ render: function() { return <this.props.component.slug className='text'> {this.props.component.value} </this.props.component.slug>; } }); React.renderComponent(<Hello component={{slug:React.DOM.div, value:'This is my header'}} />, document.body);
Here is a working fiddle: http://jsfiddle.net/kb3gN/6668/
Also, you can find JSX Compiler helpful for debugging these kind of errors: http://facebook.github.io/react/jsx-compiler.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With