简体   繁体   English

组件已安装,但我无法调用this.setState(...)或this.forceUpdate(...);

[英]Component is mounted but I can't call this.setState(…) or this.forceUpdate(…);

I am using scrollable tab view and facebook official flux solution. 我正在使用可滚动的标签视图和facebook官方助焊剂解决方案。 There are cases when a scene remains inactive (is not current selected tab) and receive a event from the store that need to update the scene, but I have this error : Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. 有些场景保持不活动状态(不是当前选中的选项卡)并从商店接收需要更新场景的事件,但我有这样的错误: Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op.

I've double checked, and componentWillUnmount() is not being called .. but I receive this warning and I can't more update the state of component. 我已经双重检查,并且没有调用componentWillUnmount() ..但是我收到此警告,我无法更新组件的状态。

Here I have some sample code of the component that has this problem being in the unselected tab: 这里我有一些组件的示例代码,该问题存在于未选中的选项卡中:

 constructor(props) {
    super(props);

    this.state = {
      isFavorite: this.isCurrentPostFavorite,
      isMounted: false,
      value: 0,
    };

    this.updateStateForFavoriteModification = this.updateStateForFavoriteModification.bind(this);
  }

  componentDidMount() {
    this.setState({isMounted: true});
    console.log('Article Scene did mount for article: '.toUpperCase() + this.props.sceneInfo.article.name);
    HotelStore.bind(HotelEvent.didModifiedFavorite, this.updateStateForFavoriteModification.bind(this));
  }

  componentWillUnmount() {
    this.setState({isMounted: false});
    console.log('Article Scene will unmount for article: '.toUpperCase() + this.props.sceneInfo.article.name);
    HotelStore.unbind(HotelEvent.didModifiedFavorite, this.updateStateForFavoriteModification);
  }

  changeFavoriteStatus() {
    if (this.state.isFavorite) {
      sendAction(HotelAction.removeFavorite, this.props.sceneInfo.article);
    }
    else {
      sendAction(HotelAction.addFavorite, this.props.sceneInfo.article);
    }
  }

  updateStateForFavoriteModification() {
    console.log('Update: '.toUpperCase() + this.props.sceneInfo.article.name);
    console.log('isMounted: '.toUpperCase() + this.state.isMounted);

    //
    //
    // Here I Get the error
    this.setState({isFavorite: this.isCurrentPostFavorite});
  }

  get isCurrentPostFavorite() {
    let matchNumber = HotelStore.favoritesPosts.filter((el: Post) => {
      return el.translationId === this.props.sceneInfo.article.translationId;
    }).length;
    let isFavorite = matchNumber > 0;
    console.log(isFavorite);
    return isFavorite;
  }

As result component did mount log console.log('Article Scene will unmount for article: '.toUpperCase() + this.props.sceneInfo.article.name); 结果组件确实挂载了log console.log('Article Scene will unmount for article: '.toUpperCase() + this.props.sceneInfo.article.name); is never shown before updating state, also before this.setState(...) the bool isMounted is always true in the log. 在更新状态之前从未显示过,在this.setState(...)之前,bool isMounted在日志中始终为true

EDIT : I know about isMounted and that's an antipatern, I've used this only to inspect in logs if the component is mounted or not, If I remove isMounted logic the error still persist. 编辑 :我知道isMounted ,这是一个反模式,我只使用它来检查日志是否安装组件,如果我删除isMounted逻辑错误仍然存​​在。

First of all, you shouldn't want to check if your component is mounted or not, it's an antipattern . 首先,您不应该检查您的组件是否已安装, 它是一个反模式

I guess the warning appears because you are using setState in componentWillUnmount. 我想警告出现是因为你在componentWillUnmount中使用了setState。 I think that the problem with this is that setState is asynchronous , so the state might not be changed inmediately when you call the function.. In this case, it might try to change the state when the component is already unmounted. 我认为这个问题是setState是异步的 ,所以当你调用函数时,状态可能不会立即改变。在这种情况下,它可能会尝试在组件已经卸载时更改状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应-this.setState和this.forceUpdate不更新状态以保存最新值 - React - this.setState and this.forceUpdate doesn't update state to hold latest values 无法在卸载的组件上调用setState(或forceUpdate) - Can't call setState (or forceUpdate) on an unmounted component 警告:无法在卸载的组件上调用setState(或forceUpdate) - Warning: Can't call setState (or forceUpdate) on an unmounted component componentDidMount:无法在卸载的组件上调用setState(或forceUpdate) - componentDidMount: Can't call setState (or forceUpdate) on an unmounted component 反应警告:无法在未安装的组件上调用 setState(或 forceUpdate) - React Warning: Can't call setState (or forceUpdate) on an unmounted component 反应-警告:无法在未安装的组件上调用setState(或forceUpdate) - React - Warning: Can't call setState (or forceUpdate) on an unmounted component 无法在已卸载的组件上调用setState(或forceUpdate)。 应对 - Can't call setState (or forceUpdate) on an unmounted component. React 无法在未安装的组件上调用 setState - Can't call setState on component that is not mounted 无法在尚未安装的组件上调用 setState - Can't call setState on a component that is not yet mounted 在this.forceUpdate()或this.setState(this.state)之后,React不重新呈现DOM - React not re-rendering DOM after this.forceUpdate() or this.setState(this.state)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM