【问题标题】:React state update from Redux causing Maximum update depth exceeded error来自 Redux 的反应状态更新导致最大更新深度超出错误
【发布时间】:2020-12-17 15:02:02
【问题描述】:

我对 React 和 Redux 还很陌生,在根据来自服务器的错误响应(使用 express)显示错误状态时我有点卡住了。

我研究过的大多数其他类似问题都与 onClick、onSubmit 或 onChange 相关,但我还没有见过像我这样的问题。

预期结果:从 store 中检索错误状态并显示在组件中

问题源于此段:

if (nextProps.errors){
  this.setState({ errors: nextProps.errors });    
}

完整的 Login.js:

import React, { Component } from "react";
import { connect } from "react-redux";
import PropTypes from "prop-types";
import "../css/login.css";
import { loginUser } from "../../redux/actions/authActions";
import TextFieldGroup from "../common/textFieldGroup";

class Login extends Component {
  constructor() {
    super();
    this.state = {
      username: '',
      password: '',
      errors: {}
    };
    
    this.onChange = this.onChange.bind(this);
    this.onSubmit = this.onSubmit.bind(this);
  }

  componentDidMount() {
    if (this.props.auth.isAuthenticated) {
      this.props.history.push('/dashboard');
    }
  }

  componentDidUpdate(nextProps) {
    if (nextProps.auth.isAuthenticated) {
      this.props.history.push('/dashboard');
    }

    if (nextProps.errors){
        this.setState({ errors: nextProps.errors });    
    }
  }

  onSubmit(e) {
    e.preventDefault();

    const userData = {
      username: this.state.username,
      password: this.state.password
    };

    this.props.loginUser(userData);
  }

  onChange(e) {
    this.setState({ [e.target.name]: e.target.value });
  }

  render() {
    const { errors } = this.state;

    return (
      <div className="login">
        <div className="container">
          <div className="row">
            <div className="col-md-8 m-auto">
              <h1 className="display-4 text-center">Log In</h1>
              <p className="lead text-center">
                
              </p>
              <form onSubmit={this.onSubmit}>
                <TextFieldGroup
                  placeholder="username"
                  name="username"
                  type="username"
                  value={this.state.username}
                  onChange={this.onChange}
                  error={errors.username}
                  isRequired
                />

                <TextFieldGroup
                  placeholder="Password"
                  name="password"
                  type="password"
                  value={this.state.password}
                  onChange={this.onChange}
                  error={errors.password}
                  isRequired
                />
                <input type="submit" className="btn btn-info btn-block mt-4" />
              </form>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

Login.propTypes = {
  loginUser: PropTypes.func.isRequired,
  auth: PropTypes.object.isRequired,
  errors: PropTypes.object.isRequired
};

const mapStateToProps = state => ({
  auth: state.auth,
  errors: state.errors
});

export default connect(mapStateToProps, { loginUser })(Login);

我试过了:

  1. 将值分配给临时变量,但这非常笨拙,并且会使代码在 我的意见。
  2. 验证它是否为空/非空,自然这加强了无限循环或验证 肯定的,因为它不为空,所以错误状态永远不会在页面上更新。

正如我在 Redux 开发工具中看到的那样,我肯定从服务器获得了正确的错误响应。

提前致谢。

【问题讨论】:

标签: javascript reactjs redux react-redux


【解决方案1】:

从以下位置更改条件:

if (nextProps.errors)

到:

if (this.props.errors !== nextProps.errors)

目前看来运行良好。 感谢@emile-bergeron 的修复

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 2021-05-15
    • 2021-10-01
    • 2019-10-09
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    相关资源
    最近更新 更多