wechat-miniprogram / wechat-miniprogram/mobx-miniprogram-bindings

清空 store 出现问题

Open
#69 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
249
Forks
25
Avg merge
2d 22h
Merged PRs (30d)
1

Description

Store.js

import { action, observable } from 'mobx-miniprogram';

export const userStore = observable({
  /** 用户信息 */
  userInfo: wx.getStorageSync(STORAGE_KEY.USER_INFO) || '',
  updateUserInfo: action(function (userInfo) {
    this.userInfo = userInfo;
    wx.setStorageSync("USER_INFO", userInfo);
  })})

1.账号 A: 登录成功获取到用户信息存储到 userInfo,个人中心显示的是 A 的信息

    this.storeBindings = createStoreBindings(this, {
      store: userStore,
      fields: ['userInfo'],
    });

2.当 A 账户切换到分包页面的时候,如果分包页面使用了 storeBindings 退出登录了,更新了 userInfo 为 ''

  storeBindings: {
    store: userStore,
    fields: ['userInfo'],
  },

3.账号 B:登录之后拿到的信息还是账号 A 的

登出部分代码:

export function loginOut(exit = true) {
  userStore.updateUserInfo('');
}

Storage 里面的值是最新的。主包界面,调用了userInfo 之后,进入到分包调用了之后就出现这样的问题了。

MobX 在多个组件/页面同时绑定同一个 observable 字段时,响应式追踪会混乱:

  • 第一个绑定:✅ 正常工作
  • 第二个绑定:⚠️ 可能工作
  • 第三个绑定:❌ 开始混乱
  • 第四个及以上:❌ 完全失效

将页面、自定义组件和 store 绑定有两种方式: behavior 绑定 和 手工绑定 。

页面使用了 behavior 绑定的时候会出现混乱,也就是步骤 2 的时候,测试中发现使用了 behavior的页面,就出现了上述现象。

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Store.js and reproduce the account A logout and account B login sequence across the main package and a subpackage. Compare behavior binding with manual storeBindings while tracing updates to userInfo and loginOut. Done means clearing the store in one bound page propagates everywhere and account B sees its own information.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.