reactjs - 将 prop 元素设置为状态
问题描述
我正在映射一个对象数组并将其作为要填充它的道具传递,如下所示。
const bottomData = [
{
name: 'Beneficial Owner Details',
id:'1',
},
{
name: 'Residential Address',
id:'2',
},]
mapData() {
return bottomData.map(list => <Card key={list.id} dataItems={list}/>)
}
这是我提取对象并根据需要填充细节的地方。
export default class Card extends Component {
constructor(props) {
super(props);
this.state = {
sectionId: ''
}
}
sectionPress() {
if (this.props.dataItems.id == "1"){
alert("ID is 1")
}
}
render() {
return (
<TouchableHighlight onPress={this.sectionPress}>
<View >
<Text>{this.props.dataItems.name}</Text>
</View>
</TouchableHighlight>
)
}
}
我的目标是显示带有被单击对象的“id”编号的警报。截至目前,我收到一条错误消息,提示“无法读取未定义的属性 dataItems”。我在这里缺少什么。我也尝试将其设置为状态,但没有奏效并给出了同样的错误。
解决方案
推荐阅读
- javascript - 寻找最接近目标的数组值总和的逻辑
- perl - 如何在草莓perl中获取最后执行命令的值
- python - 如何比较一个字符串的两列并将一列中的字符串大小写替换为另一列?
- double - SwiftUI - 显示双舍入到小数点后 2 位
- flutter - 可以在 Flutter 中的文本选择下方放置一个小部件吗?
- javascript - 如果我在屏幕上来回移动,为什么 useState 的状态会恢复为 false?
- python - 链表如何更新?
- docker - AWS 在 ec2 实例上的 docker 中启动示例网站
- c++ - C++ 双向类关联(使用前向声明)
- javascript - laravel 无法使用 ajax 服务器状态 419 发布数据