繁体   English   中英

如何在 mobx-react 中将代理转换为数组

[英]How to convert Proxy to Array in mobx-react

我在商店中定义了一个可观察变量,我尝试进行多次调用并将结果推送到数组。 当我这样做时,我发现我定义的数组是一个代理,因此我无法在我的页面中显示信息。
这是我的尝试:

class ClubListStore {
  @observable userClubList

  constructor() {
    this.userClubList = []
  }


  @action getUserClubs = () => {
    const userContract = new appchain.base.Contract(playerAbi, config.userContract)
    appchain.base.getDefaultAccount().then(sender => {
      userContract.methods.getUserClubsSize(sender).call().then((res) => {
        return res
      }).then((res) => {
          for (let i = 0; i < res; i++) {
            userContract.methods.getUserClubs(sender, i).call().then((clubAddr) => {
              this.userClubList.push(clubAddr)//push value to array here
            })
          }
        }
      })
    })
  }


}

const clubListStore = new ClubListStore()

export default clubListStore

当我 console.log 数组 userClubList 时,它显示为代理。

Proxy {0: "0xf3fA7DB0Cb79b12ca2081F9f5865f3fd01127FAd", length: 1, Symbol(mobx administration): ObservableArrayAdministration}
[[Handler]]
:
Object
[[Target]]
:
Array(1)
[[IsRevoked]]
:
false

我的问题是:
如何将此代理转换为包含我从调用中推送的值的数组?

谢谢你

使用console.log(toJS(userClubList))

https://mobx.js.org/api.html#tojs

使用Array.from()

let proxyArray = new Proxy([1, 2, 3], {});
console.log(Array.form(proxyArray));//retrun array type

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM