Reputation: 55
I have destructed the "password" and "retypepassword" properties from the state, now trying to apply a condition on them. When I am using the if and else statement it's working perfectly fine but when I am using the ternary operator it's not working. I have checked the format of the ternary operator, it's the same as I have written but still not working.
Please let me know what I am doing wrong!
SubmitHandler = e => {
e.preventDefault();
const { password, retypepassword } = this.state;
// if (password === retypepassword) {
// console.log("password not match");
// } else console.log(this.state);
password === retypepassword
? return(console.log("form submitted"))
: "password does not match";
};
Upvotes: 0
Views: 129
Reputation: 960
The problem is that you have different code in your if/else block than you have in your ternary statement.
See how in your if/else, you are not returning anything, and you are console.log()ing in both conditions?
Just make your ternary operator do the same:
SubmitHandler = e => {
e.preventDefault();
const { password, retypepassword } = this.state;
password === retypepassword
?
console.log("form submitted")
:
console.log("password does not match")
};
But for readability, I don't think ternaries are used this way, even though it technically works.
Upvotes: 0
Reputation: 27245
To match the if/else
behavior you'd do:
console.log(password === retypepassword ? "form submitted" : this.state);
Upvotes: 4