我不知道为什么,但是在我登录然后注销后,我无法在“”中的注册和登录面板之间切换AuthForm.jsx
“ this.setState
”将无法正常工作。我已经阐述了将代码分解成多个部分以查看它可能出错的地方。
登录前的控制台日志:
authToggleDisplaySignUp pressed"
authToggleDisplaySignUp true {displaySignUp: false}
authToggleDisplaySignUp pressed"
authToggleDisplaySignUp true {displaySignUp: true}
authToggleDisplaySignUp pressed"
authToggleDisplaySignUp true {displaySignUp: false}
authToggleDisplaySignUp pressed"
authToggleDisplaySignUp true {displaySignUp: true}
登录后的控制台日志:
authToggleDisplaySignUp pressed
authToggleDisplaySignUp true {displaySignUp: true}
authToggleDisplaySignUp pressed
authToggleDisplaySignUp true {displaySignUp: true}
authToggleDisplaySignUp pressed
authToggleDisplaySignUp true {displaySignUp: true}
这是“Root.jsx”
import React, { Component } from "react";
import { connect } from "react-redux";
import Home from "./Home.jsx";
import AuthForm from "./AuthForm/AuthForm.jsx";
import { fetchAuthenticated } from "../actions/account.js";
class Root extends Component {
state = {
displaySignUp: false
};
authToggleDisplaySignUp = () => {
console.log("authToggleDisplaySignUp pressed");
this.setState(
{
displaySignUp: !this.state.displaySignUp
},
() => {
console.log("authToggleDisplaySignUp true", this.state);
}
);
};
render() {
if (this.props.account.loggedIn && !this.state.displaySignUp) {
return <Home authToggleDisplaySignUp={this.authToggleDisplaySignUp} />;
} else {
return (
<AuthForm
displaySignUp={this.state.displaySignUp}
authToggleDisplaySignUp={this.authToggleDisplaySignUp}
/>
);
}
}
}
export default connect(
({ account }) => ({ account }),
{ fetchAuthenticated }
)(Root);
这是 AuthForm.jsx
import React, { Component, lazy, Suspense } from "react";
import Login from "./Login.jsx";
import Loading from "../Loading.jsx";
import Misc from "../Misc.jsx";
import "../css/AuthForm.css";
const SignUp = lazy(() => import("./SignUp.jsx"));
class AuthForm extends Component {
validateEmail = email => {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
};
get loginSignup() {
if (!this.props.displaySignUp) {
return (
<Login
validateEmail={this.validateEmail}
toggleDisplaySignUp={this.props.authToggleDisplaySignUp}
/>
);
} else {
return (
<Suspense fallback={<Loading />}>
<SignUp
validateEmail={this.validateEmail}
toggleDisplaySignUp={this.props.authToggleDisplaySignUp}
/>
</Suspense>
);
}
}
render() {
return (
<React.Fragment>
{this.displaySignUpState}
<div className="holder">
<div className="leftSide" />
<div className="rightSide">
{this.loginSignup}
<Misc />
</div>
</div>
</React.Fragment>
);
}
}
export default AuthForm;
主页.jsx
import React, { Component, lazy, Suspense } from "react";
import Loading from "./Loading.jsx";
import { connect } from "react-redux";
import { fetchInfo } from "../actions/accountInfo.js";
import history from "../history.js";
// Lazy Loading Components
const AccountInfo = lazy(AccountInfo => import("./AccountInfo.jsx"));
const NavBar = lazy(NavBar => import("./NavBar.jsx"));
const Upload = lazy(Upload => import("./Upload.jsx"));
class Home extends Component {
componentDidMount() {
this.props.fetchInfo();
this.checkStatus();
}
checkStatus = () => {
if (!this.props.account.loggedIn) {
history.push("/");
}
};
render() {
return (
<div>
<Suspense fallback={<Loading />}>
<NavBar />
</Suspense>
<p>Home Page</p>
<Suspense fallback={<Loading />}>
<AccountInfo accountInfo={this.props.accountInfo} />
</Suspense>
<br />
<hr />
<Suspense fallback={<Loading />}>
<Upload />
</Suspense>
<hr />
</div>
);
}
}
export default connect(
({ accountInfo, account }) => ({ accountInfo, account }),
{ fetchInfo }
)(Home);
我使用“道具隧道”将toggleDisplaySignUp
向下发送到登录和注册组件,它们只是向下 2 层,实际上我没有toggleDisplaySignUp
在“ Home.jsx
”中使用,我只是将它发送到那里以测试一些没有成功的东西。我已将其删除。
登录.jsx
<h4 onClick={this.props.toggleDisplaySignUp}>
<a href="#">Sign Up</a>
</h4>
注册.jsx
<h4 onClick={this.props.toggleDisplaySignUp}>
<a href="#">Login</a>
</h4>
以防万一这是 Logout 功能,它只是向服务器发出 GET 请求并将其更改this.props.account.loggedIn
为 false
class Logout extends Component {
reRoute = () => {
this.props.logout();
};
render() {
return (
<button onClick={this.reRoute}>Log Out</button>
);
}
}