【问题标题】:React Native - Can only update a mounted or mounting componentReact Native - 只能更新已安装或正在安装的组件
【发布时间】:2016-04-11 22:08:16
【问题描述】:

我尝试使用ddp 驱动程序从react-native 订阅meteor。在componentDidMount 期间,它给了我例外

Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op.

这是我的代码

    getInitialState: function() {
        return {
            dataSource: new ListView.DataSource({
                rowHasChanged: (row1, row2) => !_.isEqual(row1, row2),
            }),
            loaded: false,
        };
    },
    componentDidMount: function() {
        console.log('component mounted');
        console.log(this.props['actor']);

        ddp.initializeWithSubscribe(() => {
            ddp.subscribe('select-all-meals-by-restaurant', [this.props['actor']['obj']['_id']]);
        });

        var ddpClient = ddp.connection;
        var observer = ddpClient.observe('meals');

        observer.added = () => this.updateRows(_.cloneDeep(_.values(ddpClient.collections.meals)));
        observer.changed = () => this.updateRows(_.cloneDeep(_.values(ddpClient.collections.meals)));
        observer.removed = () => this.updateRows(_.cloneDeep(_.values(ddpClient.collections.meals)));
    },

    /*------------------------------------------------------------------------------
     * Util function for watching data
     *-----------------------------------------------------------------------------*/
    updateRows: function(rows) {
        console.log('rows :' + rows);

        this.setState({
            dataSource: this.state.dataSource.cloneWithRows(rows),
            loaded: true,
        });
    },

你能建议我解决这个问题的方法吗?

【问题讨论】:

  • 一切对我来说似乎都是完美的:|

标签: meteor react-native


【解决方案1】:

这解释了正在发生的事情以及如何解决它:

https://facebook.github.io/react/blog/2015/12/16/ismounted-antipattern.html

基本上,您在卸载组件后会收到更新。您需要使用 componentWillUnmount 取消订阅 Meteor 的更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 2017-05-29
    相关资源
    最近更新 更多