gpt4 book ai didi

javascript - react : Uncaught Error: Objects are not valid as a React child

转载 作者:行者123 更新时间:2023-11-30 09:41:34 25 4
gpt4 key购买 nike

我不太确定为什么会收到此错误。

react.js:20149 Uncaught Error: Objects are not valid as a React child (found: object with keys {showThreads}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of `CreateRow`.(…)

下面是代码:

var ShowThreads = React.createClass({

render: function() {
return(
<table>
<tbody>
{this.props.thread}
</tbody>
</table>
)
}
});

var CreateRow = React.createClass({
getInitialState: function() {
return {
threadVisible: false,
threads: ['lorem', 'ipsum', 'imperator', 'quad'],
}
},

onClick: function(){
// this.getThreads()
this.setState({threadVisible: !this.state.threadVisible})
},

render: function() {
var showThreads = this.state.threads.map((thread) => {
return (
<ShowThreads thread ={thread}/>
)
});

var rows = [(
<tr onClick={this.onClick}>
<td>{this.props.row['id']}</td>
<td>{this.props.row['email']}</td>
<td>{this.props.row['first']}</td>
<td>{this.props.row['last']}</td>
<td>{this.props.row['title']}</td>
<td>{this.props.row['company']}</td>
<td>{this.props.row['linkedin_url']}</td>
<td>{this.props.row['role']}</td>
</tr>
),(
<tr>
<td colSpan="8">
{
this.state.threadVisible
? {showThreads}
: null
}
</td>
</tr>
)]
return(
<tbody>
{rows}
</tbody>
)
},

})

当我打印出 showThreads 时,它按预期返回了一个包含 4 个对象的数组。不太确定为什么会出现该错误?目标是在 onClick 可见的每一行下方创建 4 个“线程”。

最佳答案

{showThreads} 是一个对象,而不是您期望的数组。您需要一个简单的 showThreads,因为条件已经在大括号内。

关于javascript - react : Uncaught Error: Objects are not valid as a React child,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40953741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com