ReacJS与猴面包树,如何在卸载组件后释放光标

ReacJS with Baobab, how to release cursor after the component is unmounted

本文关键字:组件 卸载 释放 光标 猴面包树 ReacJS      更新时间:2023-09-26
I am new to ReactJS and baobab. Here is what I have:

代码开始:

var stateTree = new Baobab({
  outputs: {
    campaignList: [],
    exportFields: [],
    exports: [],
    hygiene: [],
    exportFormatList: []
  }
});
var CampaignEdit = React.createClass({
  mixins: [
    stateTree.mixin,
    React.addons.LinkedStateMixin,
    SelectDropdownMixin,
    Router.State
  ],
  cursors: {
    campaignList: ['outputs','campaignList'],
    exports: ['outputs', 'exports']
  },
  componentDidMount: function() {
    this.cursors.campaignList.on('update', this.updateCampaignState);
    this.cursors.accountList.on('update', this.updateAccountState);
  },
  updateAccountState: function() {
    var accountList = deref_cursor(this.cursors.accountList, []);
    var select_options = this.state.select_options;
    for (var i=0; i<accountList.length; i++) {
      var account = accountList[i];
      select_options['owner_id'][account.id] = account.firstname + ' ' + account.lastname;
    }
    this.setState({ 'select_options': select_options});
  },
  render: function() {
    /* render the component here */
  }
});

:代码结束

但是,当用户切换到另一个组件并返回到此组件时,我收到此错误:"未捕获错误:不变冲突:替换状态(...(:只能更新已装载或已装载的组件。

This is coming up as my current component being loaded is in "UNMOUNTED" state.
My questions is that if the component is already unmounted, why is the unmounted component being reused? Is there a way to avoid this?

发现由于与游标绑定的事件而导致的问题。新增组件将卸载并解绑更新事件。