Reactjs 使用条件渲染多个组件
我的渲染函数中有一个if-else块。我总是得到一个错误,我的myattribute是一个未解析的变量Reactjs 使用条件渲染多个组件,reactjs,Reactjs,我的渲染函数中有一个if-else块。我总是得到一个错误,我的myattribute是一个未解析的变量 render: function() { return ({ this.state.something.length ? ( <h3>Selected products</h3> <Element myattribute={this.state.something}/>
render: function() {
return ({
this.state.something.length ? (
<h3>Selected products</h3>
<Element myattribute={this.state.something}/>
) : ''
})
}
render:function(){
返回({
这个,州,什么,长度(
精选产品
) : ''
})
}
原因是,您返回了多个元素。如果条件为真,请尝试以下操作:
render() {
return (
<div>
{
this.state.something.length ?
<div>
<h3>Selected products</h3>
<Element myattribute={this.state.something}/>
</div>
: ''
}
</div>
)
}
render(){
返回(
{
这个,州,什么,长度?
精选产品
: ''
}
)
}