React.js必须返回有效的React元素(或null) [英] React.js A valid React element (or null) must be returned
本文介绍了React.js必须返回有效的React元素(或null)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 React
的新手,我不知道为什么会导致错误,它抛出
I am new in React
and I am not sure why it cause error, it threw
未捕获错误:SearchBar.render():必须返回有效的React元素(或null)。您可能已经返回undefined,数组或其他一些无效对象
Uncaught Error: SearchBar.render(): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object
请注意,props.courses是 hash
in array
object like
Note that, the props.courses is a hash
inside array
object like
[
{"id": "1", "course_name": "ABC"},
{"id": "2", "course_name": "EFG"}
]
以下是代码
render () {
if (!this.props.courses)
return (
<div className="">
Loading...
</div>
);
return (
this.props.courses.map((course) => {
return (
<div> {course["course_name"]} </div>
)
})
);
}
推荐答案
渲染应始终返回单个元素。你还需要用 {}
包装.map函数。
Render should always return single element. Also you need to wrap you .map function with {}
.
render () {
if (!this.props.courses)
return (
<div className="">
Loading...
</div>
);
return (
<div>
{this.props.courses.map((course) => {
return (
<div> {course["course_name"]} </div>
)
})}
</div>
);
}
这篇关于React.js必须返回有效的React元素(或null)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文