[英]react render data array map
I have facing an issue in react js, i use array map function to render data on component but they show last value
of data.我在 react js 中遇到了一个问题,我使用数组 map function 在组件上呈现数据,但它们显示数据的
last value
。
react component反应组件
if (this.state.data
&& this.state.data.length
)
{this.state.data.map(({ full_name }, i) => (
name = (
<p key={i}>{this.state.data[i].full_name }</p>
)))
}
return (
<div className="row">
{names}
</div>
show all values
of data but they show last value
显示
all values
,但它们显示last value
names data名称数据
alex
john
smith //they show last value
what should i change in my code?我应该在我的代码中更改什么? anyone help me?
有人帮我吗?
Try this:尝试这个:
const names =
this.state.data.length > 0 &&
this.state.data.map(name => <p key={name}>{name}</p>);
return (
<div className="row">
{names}
</div>
);
edited based on comment feedback below根据下面的评论反馈编辑
const names = this.state.data.map((item) => (
<p key={item}>{item.full_name }</p>
))
or或者
const names = this.state.data.map(({full_name}) => (
<p key={full_name}>{full_name}</p>
))
here's a sandbox example这是一个沙盒示例
https://codesandbox.io/s/nostalgic-wood-9jrib?file=/src/App.js https://codesandbox.io/s/nostalgic-wood-9jrib?file=/src/App.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.