我正在尝试使用React JSX在循环内部嵌套另一个循环,代码如下:
{this.state.ans.map(function(item) {
return (
{this.state.quest.map(
function(item1) {return (item1)}
)}
{item}
)
})}
这个不起作用,有其他的建议吗?
你在第一个map
语句中忘记了包装
render() {
return (
<div>
{this.state.ans.map(item =>
<div> // this div was missing
{this.state.quest.map(quest => quest)}
{item}
</div>
)}
</div>
)
}
像这样尝试:
render(){
return (
.
.
.
{this.state.ans.map((item) => {
return (
<div>
{this.state.quest.map((item1) => {
return (item1);
}
)}
{item}
</div>
);
})}
);
}
div
(使用最新的react版本时,你不必这样做)。此外,为了让this
引用正确的上下文,使用lambda表达式。render
方法的开头添加以下语句:var that = this;
然后使用that
与return
中的function(){}
语法。