![](/img/trans.png)
[英]Uncaught TypeError: Cannot add property 0, object is not extensible at Array.push
[英]Cannot add property 0, object is not extensible when trying to push class-constructed objects indo an array list - React/NextJs
这是该函数中涉及的代码,目标是在 Material Ui 表中创建用于渲染的行。
class User {
constructor(id, name, email, measured, offset, paidAmount, status, home, misc, plane, transport, partner, isCoupon) {
this.id = id
this.name = name
this.email = email
this.measured = measured
this.offset = offset
this.paidAmount = paidAmount
this.status = status
this.home = home
this.misc = misc
this.plane = plane
this.transport = transport
this.partner = partner
this.isCoupon = isCoupon
}
}
const mountTableRows = () => {
rawData.map((item, index) => {
let user = {}
user = new User(
item["ulid"],
item["Name"],
item["Email"],
item["Emissions"],
item["Quantity"],
item["Value"],
item["Status"],
item.Rawresult["home"],
item.Rawresult["misc"],
item.Rawresult["plane"],
item.Rawresult["transport"],
item["Partner"],
item["isCoupon"]
)
usersList.push(user)
})
setIsListMounted(true)
}
useEffect(() => {
if (rawData.length > 0 && !isListMounted) {
mountTableRows()
}
}, [rawData])
我一直收到这个错误:
未处理的运行时错误类型错误:无法添加属性 0,对象不可扩展
源页面\dashboard\dashboard.js (124:22) @ eval
> 122 | item["isCoupon"] 123 | )
> > 124 | usersList.push(user)
> | ^ 125 | }) 126 | setIsListMounted(true) 127 | }
调用堆栈 mountTableRows
pages\dashboard\dashboard.js (107:16) 评估 pages\dashboard\dashboard.js (131:12)
我发现了问题。
我试图在不使用 setState 函数的情况下进入一个状态
usersList.push(user)
所以我再次阅读了我的代码并注意到我的愚蠢哈哈为了解决这个问题我只是用下面的代码替换了上面的代码
setUsersList(current=>[...current, user])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.