警告:无法在未安装的组件React Native上执行React状态更新

时间:2020-01-16 09:57:57

标签: javascript reactjs typescript react-native setstate

我不知道为什么要面对这个问题。如果有人知道如何处理,请帮助。预先感谢

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

2 个答案:

答案 0 :(得分:1)

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

<Root> <App/> </Root>

如果您已经完成此操作,请确保没有在另一个<Root>中调用<Root>。 请参阅此link

答案 1 :(得分:0)

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

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


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