Javascript Array.map日志记录,但不显示在屏幕上
我在状态对象中填充了一个数组,名为Javascript Array.map日志记录,但不显示在屏幕上,javascript,reactjs,Javascript,Reactjs,我在状态对象中填充了一个数组,名为plateHistory 一旦阵列被填充,我希望我的UI会用我期望的数据更新。然而,下面的代码并没有给我预期的结果 console.log按预期记录行,但它从不呈现。我正试图通过数组进行映射,该数组正在工作,因为控制台.log正在记录日志-但是为什么从未呈现?我认为我犯了一个基本的错误,但似乎无法发现它 请注意,行this.state.plateHistory.length从“Nope”变为正确的长度。这样就可以工作了,状态对象正在更新 <p>Rec
plateHistory
一旦阵列被填充,我希望我的UI会用我期望的数据更新。然而,下面的代码并没有给我预期的结果
console.log按预期记录行,但它从不呈现。我正试图通过数组进行映射,该数组正在工作,因为控制台.log
正在记录日志-但是为什么
从未呈现?我认为我犯了一个基本的错误,但似乎无法发现它
请注意,行this.state.plateHistory.length
从“Nope”变为正确的长度。这样就可以工作了,状态对象正在更新
<p>Recently printed plates</p>
<p>{this.state.plateHistory.length > 0 ? this.state.plateHistory[0].id : 'Nope'}</p>
{this.state.plateHistory.map((item, i) => {
<div>
<span>{item.id}</span>
<div className="col-lg-6 text-center" style={divStyle}>
<span>{item.id}</span>
{console.log(item.id)}
</div>
</div>
})
}
最近印刷的印版
{this.state.plateHistory.length>0?this.state.plateHistory[0]。id:'Nope'}
{this.state.plateHistory.map((项目,i)=>{
{item.id}
{item.id}
{console.log(item.id)}
})
}
如哈桑所述;传递给map的函数没有返回 如果使用带括号的箭头函数,则需要使用return语句:
x=>{return x+1}
如果不使用括号,则得到一条语句,该语句的结果是函数的返回值:
x=>x+1;
如果要在一条语句中返回对象文字,必须使用括号,以免将对象文字的括号与函数体的括号混淆:
x=>({xValue:x});
在您的情况下,您可以尝试删除括号:
{
this.state.plateHistory.map((item, i) =>//bracket removed
<div>
<span>{item.id}</span>
<div className="col-lg-6 text-center" style={divStyle}>
<span>{item.id}</span>
{console.log(item.id)}
</div>
</div>
/**closing bracket removed */)
}
{
this.state.plateHistory.map((项,i)=>//括号已删除
{item.id}
{item.id}
{console.log(item.id)}
/**关闭支架已拆下*/)
}
你忘了从地图回调中返回了吗?天哪!2个小时我一直在和这个战斗,你在34秒内就得到了它!:)这确实是失踪的归来!非常感谢。