gpt4 book ai didi

javascript - React - setState(...) 只能更新

转载 作者:行者123 更新时间:2023-12-01 03:17:39 24 4
gpt4 key购买 nike

我有一个问题,使用 setState 函数,这就是问题:

Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the LogIn component.

我不知道为什么会这样,这是代码:

LogIn.js(初始页面);

import React, { Component } from 'react';
import firebase from 'firebase';
import { Redirect } from 'react-router-dom';
export default class LogIn extends Component {
constructor(props) {
super(props);

this.state = {
loggedIn : false
};
}

handleAuth(){
let email = document.getElementById('email').value;
let password = document.getElementById('password').value;
let auth = firebase.auth();
let promise = auth.signInWithEmailAndPassword(email, password);
promise.then(result=>{
console.log(result.email);
});
promise.catch(error=> {
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorCode);
alert(errorMessage);
});
}
componentDidMount(){
firebase.auth().onAuthStateChanged(firebaseUser =>{
console.log(firebaseUser.email);
if (firebaseUser.email) {
this.setState({
loggedIn: true
});
}
else{
alert("No logeado");
}
});
}
render(){
if (this.state.loggedIn) {
return ( <Redirect to="/Dashboard"/> );
}
return(
<div className="container comunidadLogIn">
<div className="row" >
<div className="col-sm-2 push-5 align-self-center" >
<input type="text" name="email" id="email" placeholder="Email"/>
</div>
</div>

<div className="row" >
<div className="col-sm-2 push-5 align-self-center" >
<input type="password" name="password" id="password" placeholder="Password"/>
</div>
</div>
<div className="row" >
<div className="col-sm-2 push-5 align-self-center" >
<button className="btn btn-secondary" onClick={this.handleAuth} id="btnLogin">Iniciar sesión</button>
</div>
</div>
</div>
);
}
}

Dashboard.js(当loggedIn为true时重定向此页面);

import React, { Component } from 'react';

import Header from '../../components/Template/Header/Header.js';
import PanelUsers from '../../components/Template/Users/PanelUsers.js';
import CardGroup from '../../components/Dashboard/Cards/CardGroup.js';
import CardDecks from '../../components/Dashboard/DetailCard/CardDecks.js';

import store from '../../store';
import { Dashboard } from '../../db';

export default class LogIn extends Component {
constructor() {
Dashboard();
super();
this.state = {
dataState: []
};
}
componentDidMount(){
store.subscribe(()=>{
this.setState({
dataState: store.getState().dataDashboardReducer.data
});
});
}
componentWillUnmount(){
console.log("componente desmontado Dashboard");
}
render(){
return(
<div>
<Header />
<div className="row">
<div className="col-sm-2">
<PanelUsers/>
</div>
<div className="col-sm-6" >
<CardGroup datos= { this.state.dataState }/>
</div>
<div className="col-sm-4">
<CardDecks/>
</div>
</div>

</div>
);
}
}

当我转到另一个页面并返回登录时,就会发生这种情况

最佳答案

发生这种情况是因为您尝试在 componentWillMount() 中设置状态,而组件尚未安装。将其更改为 componentDidMount() ,它应该可以正常工作。

关于javascript - React - setState(...) 只能更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45446080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com