arrays - 反应为什么这个简单的数组映射函数没有返回任何东西?
问题描述
我有一个 React 组件为数组中的项目创建项目符号列表(称为 activePath)。该数组通过 console.log 正确报告,格式如下:
{device_id: "nlab5320a", port_id: "XGigabitEthernet0/0/3"}
数组中有六个。我做了一个 Array.isArray 只是为了确认这是一个真正的数组,它是。我正在尝试映射此数组并输出带有以下内容的项目符号列表:
activePath.map((item, i) => (
<li key={i}>{item.device_id}</li>
));
这是整个组件...
export default class ServicesInfo extends React.Component {
render() {
const { activePath } = this.props;
const runTable = activePath.map((item, i) => (
<li key={i}>{item.device_id}</li>
));
return (
<div>
<ul>{runTable}</ul>
</div>
);
}
}
ServicesInfo 是子组件。这里是父...
export default class InfoHolder extends React.Component {
constructor(props) {
super(props) {
}
render() {
return(
<div>
<p>Here is a listing of the nodes:</p>
<ServicesInfo />
</div>
)
}
一个没有文本的项目符号正在返回。我已经在这个项目中连续工作了大约 16 个小时,并且认为我只是缺少一些简单的东西。请帮忙!
解决方案
在组件中传递您的数组。还添加constructor(props){
super(props) {}
以从其他组件获取道具。
export default class InfoHolder extends React.Component {
constructor(props) {
super(props) {
this.state={
objectArray =[{device_id: "nlab5320a", port_id: "XGigabitEthernet0/0/3"}]
}
}
render() {
return(
<div>
<p>Here is a listing of the nodes:</p>
<ServicesInfo activePath={this.state.object} />
</div>
)
}
export default class ServicesInfo extends React.Component {
constructor(props) {
super(props) {
}
render() {
const { activePath } = this.props;
const runTable = activePath.map((item, i) => (
<li key={i}>{item.device_id}</li>
));
return (
<div>
<ul>{runTable}</ul>
</div>
);
}
}
推荐阅读
- javascript - 如果我们知道要在javascript中替换的字符的前缀,如何替换字符串中的几个字符
- python - Discord.py 重写 get_member() 函数,为除 bot 之外的所有用户返回 None
- server - PeerJS 服务器连接 SSL
- mysql - 带有 IF 语句的 ORDER BY 子句
- python-3.x - 无法使用 StaSh 在 Pythonista 3 上安装 notion-py 模块
- html - 将三个 div 并排对齐在一行中
- android - 如果我使用导航组件单击导航抽屉中的菜单,如何执行操作而不是移动到另一个目的地?
- java - 如何使键盘按键在每秒执行20次的方法中按下时执行一次方法?
- c# - 如何在 LightInject 中注册一个接受参数的开放泛型?
- session - 当第三方 cookie 被阻止时,YouTube 和 Gmail 如何共享登录会话