admin管理员组文章数量:1400303
There is a variable named "checkPwd" that has 3 optional values. "correct", "empty", and "invalid" The return method should return various HTML elements based on this variable. Like this.
<FormControl>
<Stack style={styles.stack}>
<Input
type="password"
placeholder="Confirm Password"
isInvalid={checkPwd == "correct" ? false : true}
/>
{
if (checkPwd == "empty") {
<FormControl.ErrorMessage>
Please confirm password.
</FormControl.ErrorMessage>
} else if(checkPwd == "invalid") {
<FormControl.ErrorMessage>
Password is not strong enough.
</FormControl.ErrorMessage>
}
}
</Stack>
</FormControl>
But this code occurs an error while piling. I hope your kind cooperations.
There is a variable named "checkPwd" that has 3 optional values. "correct", "empty", and "invalid" The return method should return various HTML elements based on this variable. Like this.
<FormControl>
<Stack style={styles.stack}>
<Input
type="password"
placeholder="Confirm Password"
isInvalid={checkPwd == "correct" ? false : true}
/>
{
if (checkPwd == "empty") {
<FormControl.ErrorMessage>
Please confirm password.
</FormControl.ErrorMessage>
} else if(checkPwd == "invalid") {
<FormControl.ErrorMessage>
Password is not strong enough.
</FormControl.ErrorMessage>
}
}
</Stack>
</FormControl>
But this code occurs an error while piling. I hope your kind cooperations.
Share Improve this question asked Aug 27, 2021 at 3:15 ComMa915ComMa915 1032 silver badges10 bronze badges 1- 1 Does this answer your question? if-else statement inside jsx: ReactJS – Emile Bergeron Commented Aug 27, 2021 at 4:50
3 Answers
Reset to default 3You can add condition like this:
{checkPwd == "empty" && (
<FormControl.ErrorMessage>
Please confirm password.
</FormControl.ErrorMessage>
)}
{checkPwd == "invalid" && (
<FormControl.ErrorMessage>
Password is not strong enough.
</FormControl.ErrorMessage>
)}
You could extract your logic out to a function, they you can write it in this if/else style. Then in the JSX you would just execute the function like {someFunction()}
export default function App() {
const checkPwd = "empty";
const someFunction = () => {
if (checkPwd === "empty") {
return <div>Please confirm password.</div>;
} else if (checkPwd === "invalid") {
return <div>Password is not strong enough.</div>;
}
return null;
};
return (
<>
<div>
<input type="password" placeholder="Confirm Password" />
{someFunction()}
</div>
</>
);
}
However a more mon approach in React would be to have a ternary operator inline in the JSX to control what renders based on the condition.
export default function App() {
const checkPwd = "empty";
return (
<>
<div>
<input type="password" placeholder="Confirm Password" />
{checkPwd === "empty" ? (
<div>Please confirm password.</div>
) : checkPwd === "invalid" ? (
<div>Password is not strong enough.</div>
) : null}
</div>
</>
);
}
And finally, if you are just mapping validation error codes to string values (Perhaps you received an ugly error code from the server but want to display something more human readable to the user), I will leave this approach here for you to consider.
const errorCodeMap = {
empty: "Please confirm password",
weak: "Password is not strong enough",
short: "Password is not long enough",
unmatched: "Both password values must match",
missingNumber: "Password must incliude a number"
};
export default function App() {
const errorCode = "short";
return (
<>
<div>
<input type="password" placeholder="Confirm Password" />
{!!errorCode && <div>{errorCodeMap[errorCode]}</div>}
</div>
</>
);
}
That is because jsx
does not support if
else
statements instead you can use the ternary
operator
{checkPwd == "empty" ? (
<FormControl.ErrorMessage>
Please confirm password.
</FormControl.ErrorMessage>
) : checkPwd == "invalid" ? (
<FormControl.ErrorMessage>
Password is not strong enough.
</FormControl.ErrorMessage>
) : null}
本文标签: javascriptHow to write JS code inside of the return method in ReactjsStack Overflow
版权声明:本文标题:javascript - How to write JS code inside of the return method in React.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744261069a2597719.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论