我對(duì) React 和 Redux 相當(dāng)陌生,并且在根據(jù)服務(wù)器的錯(cuò)誤響應(yīng)(使用 Express)顯示錯(cuò)誤狀態(tài)時(shí)遇到了一些困難。我研究過(guò)的大多數(shù)其他類似問(wèn)題都與 onClick、onSubmit 或 onChange 相關(guān),但我還沒(méi)有看到像我這樣的問(wèn)題。預(yù)期結(jié)果:從存儲(chǔ)中檢索錯(cuò)誤狀態(tài)并顯示在組件內(nèi)問(wèn)題源于此部分: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"> <input type="submit" className="btn btn-info btn-block mt-4" /> </form> </div> </div> </div> </div> ); }}我試過(guò)了:將值分配給臨時(shí)變量,但這非常笨拙,并且在我看來(lái)使代碼變得混亂。驗(yàn)證它是否為空/不為空,這自然會(huì)加強(qiáng)無(wú)限循環(huán)或積極驗(yàn)證,因?yàn)樗粸榭?,因此錯(cuò)誤狀態(tài)永遠(yuǎn)不會(huì)在頁(yè)面上更新。我確實(shí)從服務(wù)器獲得了正確的錯(cuò)誤響應(yīng),正如我在 Redux 開(kāi)發(fā)工具中看到的那樣。提前致謝。
來(lái)自 Redux 的 React 狀態(tài)更新導(dǎo)致超出最大更新深度錯(cuò)誤
互換的青春
2023-12-14 15:48:07