【发布时间】:2016-05-22 22:24:35
【问题描述】:
我的简单组件:
var AddProductForm = React.createClass({
render: function(){
return(
<form >
<input type='text' placeholder='lablbalbalbal'/>
</form>
)
}
})
我想通过 onClick 在某个确定的 div 中“渲染”第一个组件的第二个组件:
var HeaderAction = React.createClass({
render: function(){
return(
<button type="button" onClick={this.handleClick} className="btn border-slate text-slate-800 btn-flat"><i className={this.props.icon + " position-left"}></i>{this.props.name}</button>
)
},
handleClick: function(){
var component = React.createElement(this.props.action.component);
ReactDOM.render( component, document.getElementById('content'));
}
})
当我单击“HeaderAction”组件时,出现错误:
未捕获的不变违规:无效标签:
我的“组件”中的 console.log():
Object {$$typeof: Symbol(react.element), type: "<AddProductForm/>", key: null, ref: null, props: Object…}
$$typeof: Symbol(react.element)
_owner: null
_self: null
_source: null
_store: Object
key: null
props: Object
ref: null
type: "<AddProductForm/>"
__proto__: Object
如果在渲染调用中我将“component”更改为“<AddProductForm/>”,它可以正常工作,但在渲染之前使用 createElement 实例化对象则不行。
【问题讨论】:
-
什么是
this.props.action.component?渲染失败的时候? -
this.props.action.component 解析为字符串,没有失败。
标签: javascript reactjs