mobx列表已更新,但rerender无法正常工作

问题描述 投票:0回答:1

“mobx-react”:“^ 5.3.6”,“react”:“^ 16.6.3”,“antd”:“^ 3.10.3”,

我正在使用mobx-react,观察一个对象列表。列表中的val已更改,但页面没有重新呈现

数据结构

list = [{id:1 status:1,...}]

工作

this.items.replace(this.items);

没工作

 @observable.deep items = [];

@observer
class TableList extends Component {
  @observable items = [];//my list
...

  toggleOnline(data) {
    const { id, status } = data;
    const temp_status = status == 2 ? 1 : 2;
    ajax.post('page/switch', {
      id, status: temp_status
    }).then(action(() => {

      data.status = temp_status;
      message.success('操作成功');
    })).catch(err => {
      message.error(err);
    });
  }


render(){


 return

...
 <aonClick={this.toggleOnline.bind(this, record)}>{record.status == 2 || record.status == -1 ? '启用' : '停用'}</a>

...

 <Table
              rowKey="id"
              loading={loading}
              dataSource={this.items.toJS()}
              columns={this.columns}
              onChange={this.handleStandardTableChange}
            />

}

}
javascript reactjs mobx mobx-react
1个回答
0
投票

阅读本文:What does MobX react to?

部分:Incorrect: store a local reference to an observable object without tracking

const author = message.author;
autorun(() => {
    console.log(author.name)
})
message.author.name = "Sara";
message.author = { name: "John" };

将拾取第一个更改,message.author和author是同一个对象,并且.name属性在自动运行中被取消引用。但是,第二次更改将不会被选中,自动运行不会跟踪message.author关系。 Autorun仍在使用“旧”作者。

您不应该重新分配可观察变量。

© www.soinside.com 2019 - 2024. All rights reserved.