【问题标题】:Warning: Can't perform a React state update on an unmounted component React Native警告:无法对未安装的组件 React Native 执行 React 状态更新
【发布时间】:2020-01-16 09:57:57
【问题描述】:

我不知道我为什么会遇到这个问题。如果有人知道如何处理它,请提供帮助。提前致谢

Warning: Can't perform a React state update 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 %s.%s, the componentWillUnmount method,
    in ToastContainer (at connectStyle.js:392)
    in Styled(ToastContainer) (at Root.js:16)
    in RCTView (at View.js:45)
    in View (at Root.js:14)
    in Root (at connectStyle.js:392)
    in Styled(Root) (at LoginScreen.js:93)
    in LoginScreen (at SceneView.js:9)
    in SceneView (at StackViewLayout.tsx:889)
    in RCTView (at View.js:45)
    in View (at StackViewLayout.tsx:888)
    in RCTView (at View.js:45)
    in View (at StackViewLayout.tsx:887)
    in RCTView (at View.js:45)
    in View (at createAnimatedComponent.js:151)
    in AnimatedComponent (at StackViewCard.tsx:106)
    in RCTView (at View.js:45)
    in View (at createAnimatedComponent.js:151)
    in AnimatedComponent (at screens.native.js:71)
    in Screen (at StackViewCard.tsx:93)
    in Card (at createPointerEventsContainer.tsx:95)
    in Container (at StackViewLayout.tsx:975)
- node_modules\react-native\Libraries\YellowBox\YellowBox.js:59:8 in error
- node_modules\expo\build\environment\muteWarnings.fx.js:27:24 in error
- ... 12 more stack frames from framework internals

【问题讨论】:

标签: javascript reactjs typescript react-native setstate


【解决方案1】:

首先,您应该像这样用<Root> 包装您的应用组件

<Root> 
  <App/>
</Root>

如果您已经这样做了,请确保您没有在另一个 &lt;Root&gt; 中调用 &lt;Root&gt;。 参考这个link

【讨论】:

    【解决方案2】:

    该错误表示您正在尝试更新当前未安装的组件的状态。

    由于您没有共享任何代码,我只能猜测您正在执行一些异步任务(例如后端调用)并更新组件的状态,但是当调用解决时,组件是已经卸载了。查看错误,我认为您正在尝试在 Toast 组件被解除后更改其状态。


    在卸载前使用 componentWillUnmount 中止所有异步任务。

    【讨论】:

      猜你喜欢
      • 2019-09-11
      • 2021-08-07
      • 2020-06-14
      • 2021-03-27
      • 2021-02-24
      • 1970-01-01
      • 2021-11-30
      • 2020-09-03
      相关资源
      最近更新 更多