简体   繁体   English

构建一个反应应用程序但收到一条我找不到的错误消息:警告:无法在未安装的组件上执行 React state 更新

[英]Building a react app but getting an error message that I can't locate: Warning: Can't perform a React state update on an unmounted component

Im getting the below error and struggling to find the source of the problem.我收到以下错误并努力寻找问题的根源。 Based on the error I see in the console, should I investigate the lines;根据我在控制台中看到的错误,我应该调查这些行吗? Toolbar.js:15?工具栏.js:15? is there console suggesting the error is there?是否有控制台提示错误存在?

Also, what is the general origin of this type of issues?另外,这类问题的一般根源是什么?

在此处输入图像描述

Usually this happens when you try to update the state on a component that is no longer mounted, which is pretty common for asynchronous operations such as timers or requests.当您尝试在不再安装的组件上更新 state 时,通常会发生这种情况,这对于诸如计时器或请求之类的异步操作非常常见。

What some like to do is to set a flag on the componentDidMount() method and unset that flag on the componentWillUnmount() method, a flag commonly named this.mounted .有些人喜欢在 componentDidMount() 方法上设置一个标志,并在componentDidMount() componentWillUnmount()上取消设置该标志,这个标志通常命名为this.mounted With this, you can verify the state of your component before you call the setState() and make sure it is only called when the component is still mounted.有了这个,您可以在调用setState()之前验证组件的 state 并确保仅在组件仍然挂载时才调用它。

Let me know if you have any questions.如果您有任何问题,请告诉我。

Hope this was helpful.希望这会有所帮助。

暂无
暂无

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

相关问题 为什么我收到以下 React Native 警告:Can't perform a React state update on an unmounted component - Why am I getting the following React Native warning: Can't perform a React state update on an unmounted component 无法对卸载的组件警告执行React状态更新 - Can't perform a React state update on an unmounted component warning 警告:无法对未安装的组件执行 React state 更新 - Warning: Can't perform a React state update on an unmounted component React Can't perform a React state update on an unmounted component error - React Can't perform a React state update on an unmounted component error React:ComponentDidMount 中的 SetInterval 导致错误“警告:无法在未安装的组件上执行 React 状态更新。” - React: SetInterval in ComponentDidMount causing error "Warning: Can't perform a React state update on an unmounted component." “无法在未安装的组件上执行 React state 更新”错误 - The “Can't perform a React state update on an unmounted component” error 修复“无法对卸载的组件执行 React 状态更新”错误 - Fix "Can't perform a React state update on an unmounted component" error 当我使用 Async/Await 时,我收到以下警告:Can't perform a React state update on an unmounted component - When i use Async/Await i get the warning of : Can't perform a React state update on an unmounted component React - 无法在未安装的组件上执行 React state 更新 - React - Can't perform a React state update on an unmounted component React Native:无法对未安装的组件执行 React state 更新 - React Native: Can't perform a React state update on an unmounted component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM