gpt4 book ai didi

javascript - TypeError : prevState. blockHash 不可迭代

转载 作者:行者123 更新时间:2023-12-02 21:05:22 26 4
gpt4 key购买 nike

我试图更新存储在数组中的对象。但得到 TypeError: prevState.blockHash 是不可迭代的。这是我的构造函数

constructor(props) {
super(props)

this.state = {
dir:"",
account: '',
name: [],
fido: [{
logIndex: [],
transactionIndex: [],
transactionHash: [],
blockHash: []
}],
loading: true
}

我以这种方式设置状态-

showusingBot= () => {
this.setState(this.initialState)
this.state.instance.events.Artworkcreated({
filter: { purchased: false},
fromBlock: 0
}).on('data', event => {
this.setState(prevState =>({
fido:[...prevState.fido.map({blockHash: [
...prevState.blockHash,
event.blockHash
]} )]}));
})
}

我在控制台上的 ABI 响应如下。

{logIndex: 0, transactionIndex: 0, transactionHash: "0x94f6d8671988ceb8ef1da862257637a198f4afefc3aef6cf3eb992dfcafb0eb1", blockHash: "0xd26937f8535a335663c9af57335f7cc783aba0e9e376408cbb92c1b3f1b28166", blockNumber: 20, …}
logIndex: 0
transactionIndex: 0
transactionHash: "0x94f6d8671988ceb8ef1da862257637a198f4afefc3aef6cf3eb992dfcafb0eb1"
blockHash: "0xd26937f8535a335663c9af57335f7cc783aba0e9e376408cbb92c1b3f1b28166"
blockNumber: 20
address: "0x20B40e09b75a21E0B857F695dE5De92a5A5b5AD0"
type: "mined"
id: "log_0d967aac"
returnValues: Result
0: "1"
1: "bhavin"
2: "masterpiece"
3: "1000000000000000000"
4: "100"
5: "200"
6: "blah blah blah!!"
7: "0x04f78093E2a1C07BF6c4527Aaa00807d3132A1Df"
8: false
id: "1"
Artistname: "bhavin"
Artname: "masterpiece"
price: "1000000000000000000"
width: "100"
height: "200"
Description: "blah blah blah!!"
owner: "0x04f78093E2a1C07BF6c4527Aaa00807d3132A1Df"
purchased: false
__proto__: Object
event: "Artworkcreated"
signature: "0xf912339172a3b7eda9cb10ecdef181d10a74fc4411fe5d7e62f550ef3698d845"
raw: {data: "0x000000000000000000000000000000000000000000000000…16820626c6168212100000000000000000000000000000000", topics: Array(4)}
__proto__: Object

我需要将字符串推送到数组 blockHash。

最佳答案

您的整个 fido 映射回调不正确,它应该采用一个函数,而您正在传递一个对象。

this.setState(prevState =>({
fido:[...prevState.fido.map({blockHash: [
...prevState.blockHash,
event.blockHash
]} )]}));
})

正确的语法是map((current,index,originalArray) => {...})

但我认为你不需要映射任何东西,我认为你只需要扩展先前状态的 fido 数组并添加新元素

this.setState(prevState =>({
fido:[...prevState.fido, event.blockHash]
}));

编辑 1

由于 fido 是静态的,正如您所说,我建议将其属性存储在一个对象中,就像您当前将其作为数组中的元素一样。

this.state = {
dir:"",
account: '',
name: [],
fido: {
logIndex: [],
transactionIndex: [],
transactionHash: [],
blockHash: []
},
loading: true
}

现在,当更新 fido 状态时,在之前的状态中传播新元素的 blockHash 数组

this.setState(prevState =>({
fido: {
...prevState.fido,
blockHash: [...prevState.fido.blockHash, event.blockHash]
},
}));

OFC,如果您想要/需要保持原样,您需要正确访问该元素

this.setState(prevState =>({
fido: [{
...prevState.fido[0],
blockHash: [...prevState.fido[0].blockHash, event.blockHash]
}],
}));

关于javascript - TypeError : prevState. blockHash 不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61238743/

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