gpt4 book ai didi

javascript - 如何一次有条件地更新多个元素

转载 作者:行者123 更新时间:2023-12-02 21:10:08 25 4
gpt4 key购买 nike

我是 React 新手,并试图找出一次更新多个元素而不必一直重复代码的最佳方法。

我有一个登录页面,有几种状态:“登录”、“注册”或“忘记密码”。根据当前模式,我已经能够使用 renderForm() 更改使用的表单。但是,我还需要更新根组件中的一些其他文本,但不知道最有效的方法是什么。当然,我可以对我必须更改的每个文本片段重复 renderForm() ,但这似乎不太干燥。

这是我的(非常简化的)代码:

function Login() {
const [mode, setMode] = useState("LOGIN");

function renderForm(): ReactElement {
return (
<>
{
{
LOGIN: <EmailPasswordForm setMode={setMode} type="login"/>,
SIGNUP: <EmailPasswordForm setMode={setMode} type="signup"/>,
FORGOT_PASSWORD: <ForgotPassword setMode={setMode} auth={auth} />,
}[mode]
}
</>
);
}


return (
<div>
<h2>
Sign in to your account //title that needs to change depending on the mode
</h2>

{renderForm()}

<a href="/privacy">Privacy Info</a>
<p>
Some text that also needs to change depending on if the mode is "LOGIN", "SIGNUP", or "FORGOT_PASSWORD"
</p>
<OAuthSignUpComponents/>
<p>
Some more text that needs to change depending on if the mode is "LOGIN", "SIGNUP", or "FORGOT_PASSWORD"
</p>
</div>
)
}

function EmailPasswordForm({setMode, type}) {

const handleSubmit = () => {
type == "login" ? loginLogic() : signupLogic();
}

return(
<form>
<input type="email/>
<input type="password"/>
<button type="button" onClick={handleSubmit}>
{ type == "login" ? <button onClick={setMode("SIGNUP")>Sign up instead</button> : <button onClick={setMode("LOGIN")>Sign in instead</button> }
</form>
)
}

function ForgotPasswordForm({setMode}) {
return(
<form>
<input type="email/>
<button type="button">
</form>
)
}

我尝试过的一件事是在 renderForm() 中使用开关,如下所示:

const [title, setTitle] = useState("Sign in to your account");

function renderForm(){
switch(mode) {
case "LOGIN":
setTitle("Sign in to your account")
return <EmailPasswordForm setMode={setMode} type="login"/>;
case "SIGNUP":
setTitle("Sign in to your account")
return <EmailPasswordForm setMode={setMode} type="signup"/>;
case "FORGOT_PASSWORD":
setTitle("Reset Password")
return <ForgotPasswordForm setMode={setMode}/>
}
}

但这也不起作用,因为它会导致太多的重新渲染错误。

最佳答案

您应该在效果中设置 setTitle,以便不再出现渲染问题,因为您的主渲染函数中不应该有副作用,而只能在回调和效果中出现副作用。您绝对可以将渲染形式保留为对象而不是开关盒。无论哪种方式都可以。

    useEffect(() => {
switch(mode) {
case "LOGIN":
setTitle("Sign in to your account")
case "SIGNUP":
setTitle("Sign in to your account")
case "FORGOT_PASSWORD":
setTitle("Reset Password")
}
return () => {
// You can then set the original title of the application to clean up once they've logged in.
// setTitle('Original Title')
}
}, [mode])

function renderForm(){
switch(mode) {
case "LOGIN":
return <EmailPasswordForm setMode={setMode} type="login"/>;
case "SIGNUP":
return <EmailPasswordForm setMode={setMode} type="signup"/>;
case "FORGOT_PASSWORD":
return <ForgotPasswordForm setMode={setMode}/>
}
}

您甚至不必将 switch case 或对象放在内部函数中。

  const formMap={
LOGIN: <EmailPasswordForm setMode={setMode} type="login"/>,
SIGNUP: <EmailPasswordForm setMode={setMode} type="signup"/>,
FORGOT_PASSWORD: <ForgotPassword setMode={setMode} auth={auth} />,
};

return(<div>
<h2>
Sign in to your account //title that needs to change depending on the mode
</h2>

{formMap[mode]}

<a href="/privacy">Privacy Info</a>
<p>
Some text that also needs to change depending on if the mode is "LOGIN", "SIGNUP", or "FORGOT_PASSWORD"
</p>
<OAuthSignUpComponents/>
<p>
Some more text that needs to change depending on if the mode is "LOGIN", "SIGNUP", or "FORGOT_PASSWORD"
</p>
</div>)

关于javascript - 如何一次有条件地更新多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61120923/

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