Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 使用条件渲染多个组件_Reactjs - Fatal编程技术网

Reactjs 使用条件渲染多个组件

Reactjs 使用条件渲染多个组件,reactjs,Reactjs,我的渲染函数中有一个if-else块。我总是得到一个错误,我的myattribute是一个未解析的变量 render: function() { return ({ this.state.something.length ? ( <h3>Selected products</h3> <Element myattribute={this.state.something}/>

我的渲染函数中有一个if-else块。我总是得到一个错误,我的myattribute是一个未解析的变量

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(){
返回(
{
这个,州,什么,长度?
精选产品
: ''
}
)
}