【发布时间】:2020-11-13 16:43:23
【问题描述】:
我的组件:
import React, { useEffect, useState } from 'react'
import './viewAcc.css'
import HttpService from '../../services/http-service'
const http = new HttpService()
function ViewAcc({match}){
useEffect(()=>{
getAcc()
}, [])
const [acc, setAcc] = useState({})
const getAcc = async()=>{
const data = await http.getAcc(match.params.id)
setAcc(data[0])
}
return(
<div style={{display: "block"}} className="container">
<div className="row">
<div className="col-8" style={{alignItems:"center", marginBottom:"40px"}}>
<h3><b>Name: {"\u00a0\u00a0"}</b> {acc.name}</h3>
<div style={{display:"flex", alignItems:"center"}}>
<h3><b>Phone Nos: </b></h3>
{acc.ph_nos.map(i =>
<p key={i}>{i}</p>
)}
</div>
</div>
<div className="col-sm">
<input></input>
</div>
</div>
</div>
)
}
export default ViewAcc
当我编写这段代码并且 npm 自动刷新时,姓名和电话号码都显示了(这意味着正在获取数据)但是当我返回主页并单击我的动态路由器链接时,
TypeError: acc.ph_nos is undefined 显示错误。
当我删除 acc.ph_nos.map 部分时,名称显示没有错误。我该如何解决这个问题?
【问题讨论】: