简体   繁体   English

添加等待标记以调度后,无法对未安装的组件执行反应 state 更新

[英]Can't perform a react state update on an unmounted component after adding await tag to dispatch

I have added an await tag to a redux dispatch and now I receive this error:我在 redux 调度中添加了一个等待标签,现在我收到此错误:

index.js:1 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 a useEffect cleanup function.
    in LoginForm (created by ConnectFunction)
    in ConnectFunction (at login.js:12)
    in div (at login.js:11)

Can someone please explain why this is occurring and how I can go about 'cleaning up'my asynchronous task?有人可以解释为什么会发生这种情况以及我如何 go 关于“清理”我的异步任务? I don't understand which component has not been mounted我不明白哪个组件没有安装

My code:我的代码:

login.js:登录.js:

import React, { Component } from 'react';
import LoginForm from '../components/form/loginForm';
import { connect } from 'react-redux';
import { SUCCESS, HOME } from '../common/webUtils';
import { Redirect } from 'react-router-dom';

class Login extends Component {
  render() {
    if( this.props.loginStatus === SUCCESS ) return <Redirect to = {HOME}/>
    else return (
      <div style = {{paddingTop: "180px", background: 'radial-gradient(circle, rgba(106,103,103,1) 0%, rgba(36,36,36,1) 100%)', height: "100vh"}}>
        <LoginForm/>
      </div>
    );
  }
}

function mapStateToProps( state ) {
  return {
    loginStatus: state.userReducer.loginStatus
  }
}

export default connect ( mapStateToProps ) ( Login );

Dispatch code that causes the warning from loginForm.js:导致 loginForm.js 警告的调度代码:

async function handleSubmit() {
    setSubmitted( true )
    if( isValidForm() ) {
      const details = {
          "username": userName,
          "password": password
      }
      await props.login( details )
      if( props.loginStatus !== SUCCESS ) handleShowError()
    } else { 
      handleShowError()
    }
  }

setShowError function and corresponding state value initialisation: setShowError function 和相应的 state 值初始化:

const [showError, setShowError] = React.useState( false )

const handleShowError = () => {
    setShowError( true )
  };

it seems the login is not get notified by "async function handleSubmit" result I believe this the reason the component is not get mounted.似乎“异步 function handleSubmit”结果未通知登录我相信这是组件未安装的原因。

In general, with redux, you need middleware such thunk or saga to manage the service workers in the background.一般来说,使用 redux,您需要中间件,例如 thunk 或 saga 来管理后台的服务人员。

Redux alone cannot do this job.仅 Redux 无法完成这项工作。

暂无
暂无

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

相关问题 当我使用 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 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 Native:无法对未安装的组件执行 React state 更新 - React Native: Can't perform a React state update on an unmounted component React 导致:无法对未安装的组件执行 React state 更新 - React causing: 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 状态更新 - Navigation issue - can't perform a React state update on unmounted component 如何解决“无法对未安装的组件执行反应状态更新” - How to solve "can't perform a react state update on an unmounted component" 无法在未安装的组件主题提供程序上执行 React state 更新 - Can't perform a React state update on an unmounted component theme provider 无法使用 useEffect 挂钩对未安装的组件执行 React state 更新 - Can't perform a React state update on an unmounted component with useEffect hook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM