dynamic - Create an instance of a React class from a string -


i have string contains name of class (this coming json file). string tells template class layout / template use data (also in json). issue layout not displaying.

home.jsx:

//a template or layout. var home = react.createclass({   render () {     return (     <div>home layout</div>     )   } }); 

template.jsx:

var template = react.createclass({   render: function() {     var tag = this.props.template; //this name of class eg. 'home'     return (         <tag />     );   } }); 

i don't errors don't see layout / home class. i've checked props.template , logs correct info. also, can see home element in dom. looks this:

<div id='template-holder>     <home></home> </div> 

if change following line to:

var tag = home; //this works it's not dynamic! 

any ideas, how can fix this? i'm sure it's either simple fix or i'm doing stupid. appreciated. apologies if has been asked (i couldn't find it).

thanks, ewan

this not work:

var home = react.createclass({ ... });  var component = "home"; react.render(<component />, ...); 

however, will:

var home = react.createclass({ ... });  var component = home; react.render(<component />, ...); 

so need find way map between string "home" , component class home. simple object work basic registry, , can build there if need more features.

var components = {   "home": home,   "other": othercomponent };  var component = components[this.props.template]; 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -