我试图用 React 渲染一个非常简单的表格,但它提示得很厉害:
“不变违规:Table.render():必须返回有效的 ReactComponent。您可能返回了未定义的数组或其他无效对象。”
定义如下:
var Table = React.createClass({
render: function() {
return
(<table class="table table-striped">
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>One</td>
<td>Two</td>
</tr>
<tr>
<td>One</td>
<td>Two</td>
</tr>
</tbody>
</table>);
}
});
返回的是单个元素,HTML 是有效的,所以我不确定问题是什么。有人知道是什么问题吗?
最佳答案
向evan致敬,谁在评论中实际提供了答案。
我只是被 Javascript 的“自动”分号插入绊倒了。单个 return 语句意味着从函数返回,给它一个未定义的返回值。以下 jsx 从未被执行。
在返回的同一行启动 jsx 可确保返回按预期执行。
关于javascript - 简单的 ReactJS 组件渲染无效 - 原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28159744/