React Components条件渲染,句柄单击函数中的三级运算符渲染错误组件的问题
我是一名新开发人员,这是我关于堆栈溢出的第一个问题,所以请耐心等待。我希望这是正确的社区和正确的提问方式(如果有方法可以改善我的询问,请告诉我)。
所以我有一个 React 功能组件,可以有条件地呈现三种形式。我采用了一个大表单并将其分成三个部分,以便当用户单击 div 中的下一个按钮时,他们将被带到下一个表单,下一个按钮调用一个单击处理程序函数,该函数使用状态来确定应显示哪个表单使用第三级操作员来检查状态。我的问题是,处理程序函数的逻辑在第一次单击后显示第三种形式,而不是我所期望的第二种形式,是否有更简单的解决方法或如何解决此问题?我有下面的代码并尽可能地简化它,以尝试仅显示我正在处理的逻辑问题。
//代码
import { useState } from "react";
export default function PinFormTest(){
const [showFirstForm, setShowFirstForm] = useState(true)
const [showSecondForm, setShowSecondForm] = useState(false)
const [showThirdForm, setShowThirdForm] = useState(false)
const handleNextForm = () => {
showFirstForm ? setShowFirstForm(false) && setShowSecondForm(true):
showSecondForm ? setShowSecondForm(false) && setShowThirdForm(true):
console.log("error")
}
return(
<>
<div className="formContainer">
{showFirstForm ?
(<>firstForm</>): showSecondForm ? (<>secondForm</>): (<>thirdForm</>)
}
</div>
<div>
<button onClick={() => handlePreviousForm()}>Previous</button>
<button onClick={() => handleNextForm()}>Next</button>
</div>
</>)
}
I'm a new developer and this is my first question on stack overflow so please be patient. I hope this is the right community and the correct way to ask a question (let me know if there are ways I can imporve my inquiries).
So I have a React functional compoenent that renders three forms conditionally. I took one big form and broke it into three sections so that when the user clicks the next button in the div, they are taken to the next form and the next button calls a click handler function that uses state to determine which form should be shown with a tertiary operator to check the state. My problem is that the logic of the handler function shows the third form after first click instead of the second as I would expect, any idea of a simpler work around or how to fix this? I have the code below and simplified it as much as I could to try and just show the logic issue I'm dealing with.
//Code
import { useState } from "react";
export default function PinFormTest(){
const [showFirstForm, setShowFirstForm] = useState(true)
const [showSecondForm, setShowSecondForm] = useState(false)
const [showThirdForm, setShowThirdForm] = useState(false)
const handleNextForm = () => {
showFirstForm ? setShowFirstForm(false) && setShowSecondForm(true):
showSecondForm ? setShowSecondForm(false) && setShowThirdForm(true):
console.log("error")
}
return(
<>
<div className="formContainer">
{showFirstForm ?
(<>firstForm</>): showSecondForm ? (<>secondForm</>): (<>thirdForm</>)
}
</div>
<div>
<button onClick={() => handlePreviousForm()}>Previous</button>
<button onClick={() => handleNextForm()}>Next</button>
</div>
</>)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该通过编写逗号分隔的语句来编写三元运营商中的多行语句。您的正确代码将是;
You should write multiple lines of statements in ternary operators by writing comma separated statements. Your correct code will be;
在这种情况下,尽量避免使用三元运算符,因为它会导致可读性等问题,从而使以后的调试变得更加困难。
In such situations try to avoid using the ternary operator because it will cause issues like readability, which makes it harder to debug later on.
您正在使用
&&
并且仅当left
为true
时才会评估right
所以您正在使用
setShowFirstForm(false) && setShowSecondForm(true)
因此,这将首先评估setShowFirstForm(false)
,您不会从中返回任何内容,因此返回值将为undefined
这是一个虚假
值。因此,这将评估setShowSecondForm(true)
解决方案 1
您可以在此处使用
,
: Codesandbox 演示解决方案2
您可以使用简单的
if-else
分支,Codesandbox demo解决方案3
这里可以使用
Map
来创建地图对于表单级别和要渲染的组件,如果表单中的阶段增加,其他解决方案将不会是更好的解决方案。 Codesandbox 链接解决方案4 - 最佳解决方案
创建一个自定义挂钩,用于处理
下一个
和上一个
。您所要做的就是传递需要顺序渲染的组件数组。 Codesandbox 链接You are using
&&
and it will only evaluateright
only if theleft
istrue
So you are using
setShowFirstForm(false) && setShowSecondForm(true)
so this will first evaluatesetShowFirstForm(false)
from which you are not returning anything so the return value will beundefined
which is afalsy
value. So this will evaluatesetShowSecondForm(true)
Solution 1
You can use
,
here as: Codesandbox demoSolution 2
You can use simple
if-else
branching, Codesandbox demoSolution 3
You can use
Map
here to create a map for form level and the component to render, other solution won't be a better solution if the stages in form increases. Codesandbox linkSolution 4 - BEST SULUTION
Create a custom hook as which will handle the
next
andprevious
. All you have to do is to pass an array of components that needs to be rendered sequentially. Codesandbox link