世界上最伟大的投资就是投资自己的教育

全场限时 5 折

首页React
magic · 真仙

[mobx] 登录状态维护相关 action reaction 报错问题 烦请各位高人看一下

magic发布于1738 次阅读

代码

  • configure({
        enforceActions: 'always'
    })
    
    ......
    @action.bound
      setToken(value) {
      this.token = value
    }
    
    @action.bound
    setUserInfo(value) {
      this.userInfo = value
    }
    
    @action.bound
    setAccountStatus(value) {
      this.account_status = value
    }
    
    @action.bound
    async doWeappLogin(body) {
        this.toggleLoading(true)
    
        try {
        const resp = await miniAppLogin(body)
    
        if (resp) {
            const { token, needBind } = resp
            this.setToken(token)
            const userInfo = await getUserInfo()
            this.setUserInfo(userInfo)
            this.setAccountStatus(true)
            return
        }
        }catch(e) {
            console.log(e)
        }
    }
    

    报错信息

    • Since strict-mode is enabled, changing observed observable values outside actions is not allowed. Please wrap the code in anactionif this change is intended. ## 尝试办法
    • 将 enforceActions 变成 observe nerver 依然报错
    • javascript // 用runInAction包裹依然报错 const _this = this runInAction(async() : { _this.setToken(token) const userInfo = await getUserInfo() _this.setUserInfo(userInfo) _this.setAccountStatus(true) }) ## 期待的结果
  • 消除报错, mobx 一报错就会阻止视图渲染, 但是值确实是改过来了

本站文章均为原创内容,如需转载请注明出处,谢谢。

1 条回复
喜欢
统计信息
    学员: 29065
    视频数量: 1973
    文章数量: 489

© 汕尾市求知科技有限公司 | Rails365 Gitlab | Qiuzhi99 Gitlab | 知乎 | b 站 | 搜索

粤公网安备 44152102000088号粤公网安备 44152102000088号 | 粤ICP备19038915号

Top