【发布时间】:2022-06-13 17:17:42
【问题描述】:
我正在从底部的代码生成登录状态。然后,我将生成的状态(真或假)提升到父组件(App.js 文件),然后进一步尝试实现条件渲染,但它的行为不符合预期。有什么建议吗?
我已附上以下文件。我的网站仍然显示登录页面,并且从不呈现其他条件内容。
App.js 文件
import "./App.css";
import LoginPage from "./Login/LoginPage";
import NewPost from "./Posts/NewPost";
import Posts from "./Posts/Posts";
import PostList from "./Posts/PostList";
import { useState } from "react";
const expenses = [
{
name: "Lakshay Gupta",
content:
" Amet minim mollit non deserunt ullamco est sit aliqua dolor do amet sint. Velit officia consequat duis enim velit mollit. Exercitation veniam consequat sunt nostrud amet.",
posted: "5mins ago",
comments: "16 comments",
},
{
name: "Naman Sukhija",
content:
" Amet minim mollit non deserunt ullamco est sit aliqua dolor do amet sint. Velit officia consequat duis enim velit mollit. Exercitation veniam consequat sunt nostrud amet.",
posted: "1hour ago",
comments: "24 comments",
},
{
name: "William Harris",
content:
" Amet minim mollit non deserunt ullamco est sit aliqua dolor do amet sint. Velit officia consequat duis enim velit mollit. Exercitation veniam consequat sunt nostrud amet.",
posted: "3mins ago",
comments: "29 comments",
},
];
function App() {
let loggedIn = false;
const submitLoginHandler = (ifLoggedIn) => {
loggedIn= ifLoggedIn;
console.log(loggedIn);
};
return (
<div className="App">
{!loggedIn ? (
<LoginPage onSubmitLogin={submitLoginHandler}></LoginPage>
) : (
<div>
<NewPost></NewPost>
<PostList items={expenses}></PostList>{" "}
</div>
)}
</div>
);
}
export default App;
LoginPage.js 文件
import React from "react";
import "./LoginPage.css";
import { useState } from "react";
export default function LoginPage(props) {
const [loggedIn, setLoggedIn] = useState(false);
const loginHandler = (e) => {
e.preventDefault();
setLoggedIn(true);
props.onSubmitLogin(loggedIn);
}
return (
<div>
<form className="form-dimensions">
<div className="mb-4 custom-heading">
WELCOME BACK
</div>
<div className="mb-4 custom-subheading">
Login into your account
</div>
<div className="mb-3">
<label htmlFor="exampleInputEmail1" className="form-label email-custom form-color">
Email or Username
</label>
<input
type="email"
className="form-control"
id="exampleInputEmail1"
aria-describedby="emailHelp"
placeholder="Enter your email or username"
/>
</div>
<div className="mb-3">
<div className="label-inline">
<label htmlFor="exampleInputPassword1" className="form-label form-color password-custom label-inline">
Password
</label>
<label htmlFor="exampleInputPassword2" className="forgot-password-custom form-label form-color label-inline">
Forgot password?
</label>
</div>
<input
type="password"
className="form-control"
id="exampleInputPassword1"
placeholder="Enter your password"
/>
</div>
<button type="submit" className="btn btn-primary" onClick={loginHandler} >
Login now
</button>
<div className="custom-ending">
Not registered yet? <span>Register →</span>
</div>
</form>
</div>
);
}
【问题讨论】:
-
您还没有解除状态,您只是在父级中声明了一个变量,每次渲染都会重新声明。将
useState声明移动到父组件,并将setLoggedIn方法传递给登录组件。 -
你在任何意义上都没有提升状态。 1. 你的 App 有
let loggedIn = false;而不是状态 2. 你的 Login 组件有自己的状态 -
@pilchard 我试图在单击按钮时从登录组件生成“真”或“假”状态。然后我试图将该状态传递给父组件。
标签: javascript reactjs frontend