Checking for the existence of this.state.userAge twice is a bit of a waste. It could be simplified to
this.state.userAge ?
<<check the age>>
:
<<render buttonOne>>
I assume that you’re multiplying userAge by 1 to coerce the string into a number, but that’s not idiomatic JavaScript. Using Number is best to communicate your intention, and + is the normal shorthand. Here’s my solution:
Hi there PortableStick,
Thanks for your response!
Would you please explain the logic of your code ?
Is this.state.userAge ? equivalent to checking if the page loaded?
How would you have written the less elegant solution?
EDIT:
After some more searching found the answer.
PortableStick’s code is equivalent to:
this.state.userAge?
(this.state.userAge >= 18 ? buttonTwo : buttonThree)
: buttonOne
However, I still don’t understand what this.state.userAge? checks for.
Is it checking if an value exists?
this.state.userAge is the value that we pass onto the setState when we enter the age in the input box in the console.We take this value from the above state that is the handleChange.In other words the value is just passed down to the submit method from the handleChange method.If that age is greater than 18 then button two should be shown otherwise button one should be shown. It is not checking the truthfulness by itself. It is passed at a place in a ternary operator which when true executes the condition next to the ? if not it executes the condition next to the colon.
Hope that clears things up a bit.