调用处置与componentWillUnMount的订阅不起作用

时间:2019-02-27 03:51:37

标签: react-native relay

早晨 我有中继订阅设置,所以我突然开始收到此错误:

 Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.

所以我尝试实施一些无法解决我的问题的解决方案: 这是我的代码:

network.js

const networkSubscriptions = async (config, variables, cacheConfig, observer) => {
  const query = config.text;
  let token = await accessHelper();
  if (token != null || token != undefined) {
    const subscriptionClient = new SubscriptionClient(`ws://${api}/graphql`,
      {
        reconnect: true,
        connectionParams: {
          Authorization: token,
        },
      });
    const client = subscriptionClient.subscribe({ query, variables }, (error, result) => {
      observer.onNext({ data: result })
    })
    return {
      dispose: client.unsubscribe
    };
  }
}

subscription.js:

import {
  graphql,
  requestSubscription
} from 'react-relay'
import environment from '../network';

const subscription = graphql`
  subscription chatCreatedSubscription{
    chatCreated{
      id
      initiate_time
      update_time
      support_id
      category_id
      email
      name
    }
  }
  `;

function chatCreated(callback) {
  const variables = {};
  requestSubscription(environment, {
    subscription,
    variables,
    onError: (error)=> {
      console.log(error, "error");
    },
    onNext: () => {
      callback()
    },
    updater: () => {
      console.log("updater");
    }
  });
}

module.exports = chatCreated;

用于调用订阅的代码:

componentDidMount() {
    this.chat = chatCreated(this._refetch);
  }
componentWillUnmount() {
   this.chat.dispose()
}

但是现在this.chat.dispose()引发以下错误:

TypeError: TypeError: TypeError: TypeError: undefined is not an object (evaluating 'this.chat.dispose')

任何帮助和建议,我们感激不尽。谢谢!!

0 个答案:

没有答案