【发布时间】:2021-07-06 17:16:26
【问题描述】:
我是 React 新手,正在尝试将对象数组传递给子组件。但是子组件什么也不渲染。下面是我的代码
This is my array in home.js
board: [
{
name: 'todo',
tasks: [
{
description: '',
name: 'first task',
id: uuid(),
userAssigned: null
},
{
description: '',
name: 'second task',
id: uuid(),
userAssigned: null
},
{
description: '',
name: 'and thrid',
id: uuid(),
userAssigned: null
}
]
},
{
name: 'in-progress',
tasks: [
{
description: '',
name: 'first task',
id: uuid(),
userAssigned: null
}
]
},
{
name: 'done',
tasks: [
{
description: '',
name: 'first task',
id: uuid(),
userAssigned: null
}
]
}
],
这就是我渲染数组的方式
<Row>
{
this.state.board.map(cname => (
<Col key={cname.name} md="2">
<Card border="dark">
<Card.Title className="text-center py-2 mb-0">{cname.name}</Card.Title>
<Card.Text>
<Task tasks={cname.tasks} />
</Card.Text>
</Card>
</Col>
))
}
</Row>
所以这里Task是一个子组件它如下
import React, { Component } from 'react';
import {Card} from 'react-bootstrap';
class TaskCard extends Component {
constructor(props) {
super(props);
}
render() {
return (
<template>
{
this.props.tasks.map(element => {
<Card border="dark">
<Card.Text>{element.name}</Card.Text>
</Card>
})
}
</template >
);
}
}
export default TaskCard
现在在上面的子组件代码中,它不显示数组中所述的任务名称。我需要在卡片文本中显示任务-> 名称。
【问题讨论】: