gpt4 book ai didi

javascript - 尝试在数组内映射数组并显示在表格中

转载 作者:行者123 更新时间:2023-12-03 22:32:04 24 4
gpt4 key购买 nike

我目前在我的数据库中有三个表。我有一个用户、医学和医学详细信息表。它们是这样相关的,药物细节属于药物,药物属于用户。
我目前正在使用 sequelized 来调用我的数据库并获取用户和属于用户的嵌套表。在 react 中,我需要对对象中的 Medicine 数组进行 map 以获取所有药物名称,然后我想对 med details 数组进行 map 以获取服用时间。

                <tbody>
{ user.Medicines && user.Medicines.length !== 0 && user.Medicines.map(item => {
console.log(item)


const medDeets = item.MedDeets
const medDeetMap = medDeets.map(deet => {
const time = deet.timeTaken

// setTime(time)
Issue here>? })
return (
<tr>
<td>{item.medicineName}</td>
<td> </td>
<td></td>
<td>{item.waitingPeriod} hours</td>
</tr>

)
// }


})}
</tbody>
如果我尝试从我的第二个 }) 函数(也显示在上面的代码中)中移动 map,我似乎遇到了范围问题,并且无法再看到我的第一个 map 函数中的数据。如果我尝试从第二个 setState 函数中 map ,我会收到太多的重新渲染错误。
目前是全栈开发程序的新毕业生,这是我第一次使用 Stackoverflow 如果我遗漏了一些明显的东西,我很抱歉。感谢帮助/反馈。

最佳答案

You can not setstate inside render function because it will causeside effect. What exactly happens is that each time you update statereact calls render function, so if you will update state inside renderfunction then it will stuck inside infinite loop

 <tbody>
{ user.Medicines && user.Medicines.length !== 0 && user.Medicines.map(item => {
console.log(item)


const medDeets = item.MedDeets
const medDeetMap = medDeets.map(deet => {
const time = deet.timeTaken
return time ;
})
return (
<tr>
<td>{medDeetMap .medicineName}</td>
<td> </td>
<td></td>
<td>{medDeetMap .waitingPeriod} hours</td>
</tr>

)
// }


})}
</tbody>

关于javascript - 尝试在数组内映射数组并显示在表格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66021897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com