【发布时间】:2021-02-16 11:41:26
【问题描述】:
我有 5 个从 API 返回的持卡人,我需要渲染所有 5 个持卡人,但它只渲染第一个持卡人。当我记录持卡人时,它正在记录 5 个持卡人的数组。但是在我映射它们并在我的renderCardHolders 中返回 JSX 之后,它只呈现第一个返回的用户。有人可以帮我吗?
function UserContainer (props) {
const [cardHolders, setCardHolders] = useState([{}])
useEffect(() => {
props.fetchedData.map(user => {
try {
const uuid = user.Guid
const response = Api.get(`/securityCenterApi/getCredentials/${uuid}`)
.then(response => {
const cardHolderCredentials = response.data.Rsp.Result.Credentials
const newGuid = Api.get(`/securityCenterApi/getUsers/${cardHolderCredentials}`)
.then(response => {
const userName = response.data.Rsp.Result.Cardholder.Name
const bitLength = response.data.Rsp.Result.Format.BitLength
const facilityCode = response.data.Rsp.Result.Format.Facility
const cardNumber = response.data.Rsp.Result.Format.CardId
setCardHolders([{
username: userName,
bitLength: bitLength,
facilityCode: facilityCode,
cardNumber: cardNumber
}])
}
)
})
} catch (err) {
console.log(err)
}
})
}, [props.fetchedData])
// console.log(cardHolders)
const renderCardHolders = () => {
return cardHolders.map((cardHolder, index) => {
console.log(cardHolder)
return (
<tr key={index}>
<th>{cardHolder.username}</th>
<td>{cardHolder.bitLength}</td>
<td>{cardHolder.facilityCode}</td>
<td>{cardHolder.cardNumber}</td>
</tr>
)
})
}
return (
<table className="table">
<thead>
<tr>
<th><abbr title="Position">Cardholder name</abbr></th>
<th>Wiegand Bit type</th>
<th><abbr title="Played">Facility Code</abbr></th>
<th><abbr title="Won">Card number</abbr></th>
</tr>
</thead>
<tbody>
{renderCardHolders()}
</tbody>
</table>
)
}
export default UserContainer
提前致谢!
【问题讨论】:
标签: reactjs react-hooks jsx