2018-10-04 14:37:37 +01:00
---
id: 5a24c314108439a4d4036187
title: Use a Ternary Expression for Conditional Rendering
challengeType: 6
2019-08-05 09:17:33 -07:00
forumTopicId: 301414
2021-01-13 03:31:00 +01:00
dashedName: use-a-ternary-expression-for-conditional-rendering
2018-10-04 14:37:37 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2020-09-03 01:24:22 +01:00
2020-11-27 19:02:05 +01:00
Before moving on to dynamic rendering techniques, there's one last way to use built-in JavaScript conditionals to render what you want: the < dfn > ternary operator</ dfn > . The ternary operator is often utilized as a shortcut for `if/else` statements in JavaScript. They're not quite as robust as traditional `if/else` statements, but they are very popular among React developers. One reason for this is because of how JSX is compiled, `if/else` statements can't be inserted directly into JSX code. You might have noticed this a couple challenges ago — when an `if/else` statement was required, it was always *outside* the `return` statement. Ternary expressions can be an excellent alternative if you want to implement conditional logic within your JSX. Recall that a ternary operator has three parts, but you can combine several ternary expressions together. Here's the basic syntax:
2019-05-14 05:01:32 -07:00
2020-07-13 18:58:50 +02:00
```jsx
2020-09-03 01:24:22 +01:00
condition ? expressionIfTrue : expressionIfFalse;
2019-05-14 05:01:32 -07:00
```
2020-11-27 19:02:05 +01:00
# --instructions--
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
The code editor has three constants defined within the `CheckUserAge` component's `render()` method. They are called `buttonOne` , `buttonTwo` , and `buttonThree` . Each of these is assigned a simple JSX expression representing a button element. First, initialize the state of `CheckUserAge` with `input` and `userAge` both set to values of an empty string.
Once the component is rendering information to the page, users should have a way to interact with it. Within the component's `return` statement, set up a ternary expression that implements the following logic: when the page first loads, render the submit button, `buttonOne` , to the page. Then, when a user enters their age and clicks the button, render a different button based on the age. If a user enters a number less than `18` , render `buttonThree` . If a user enters a number greater than or equal to `18` , render `buttonTwo` .
# --hints--
The `CheckUserAge` component should render with a single `input` element and a single `button` element.
```js
assert(
Enzyme.mount(React.createElement(CheckUserAge)).find('div').find('input')
.length === 1 & &
Enzyme.mount(React.createElement(CheckUserAge)).find('div').find('button')
.length === 1
);
```
The `CheckUserAge` component's state should be initialized with a property of `userAge` and a property of `input` , both set to a value of an empty string.
```js
assert(
Enzyme.mount(React.createElement(CheckUserAge)).state().input === '' & &
Enzyme.mount(React.createElement(CheckUserAge)).state().userAge === ''
);
```
When the `CheckUserAge` component is first rendered to the DOM, the `button` 's inner text should be Submit.
```js
assert(
Enzyme.mount(React.createElement(CheckUserAge)).find('button').text() ===
'Submit'
);
```
When a number of less than 18 is entered into the `input` element and the `button` is clicked, the `button` 's inner text should read `You Shall Not Pass` .
```js
(() => {
const mockedComponent = Enzyme.mount(React.createElement(CheckUserAge));
const initialButton = mockedComponent.find('button').text();
const enter3AndClickButton = () => {
mockedComponent
.find('input')
.simulate('change', { target: { value: '3' } });
mockedComponent.find('button').simulate('click');
mockedComponent.update();
return mockedComponent.find('button').text();
};
const enter17AndClickButton = () => {
mockedComponent
.find('input')
.simulate('change', { target: { value: '17' } });
mockedComponent.find('button').simulate('click');
mockedComponent.update();
return mockedComponent.find('button').text();
};
const userAge3 = enter3AndClickButton();
const userAge17 = enter17AndClickButton();
assert(
initialButton === 'Submit' & &
userAge3 === 'You Shall Not Pass' & &
userAge17 === 'You Shall Not Pass'
);
})();
```
When a number greater than or equal to 18 is entered into the `input` element and the `button` is clicked, the `button` 's inner text should read `You May Enter` .
```js
(() => {
const mockedComponent = Enzyme.mount(React.createElement(CheckUserAge));
const initialButton = mockedComponent.find('button').text();
const enter18AndClickButton = () => {
mockedComponent
.find('input')
.simulate('change', { target: { value: '18' } });
mockedComponent.find('button').simulate('click');
mockedComponent.update();
return mockedComponent.find('button').text();
};
const enter35AndClickButton = () => {
mockedComponent
.find('input')
.simulate('change', { target: { value: '35' } });
mockedComponent.find('button').simulate('click');
mockedComponent.update();
return mockedComponent.find('button').text();
};
const userAge18 = enter18AndClickButton();
const userAge35 = enter35AndClickButton();
assert(
initialButton === 'Submit' & &
userAge18 === 'You May Enter' & &
userAge35 === 'You May Enter'
);
})();
```
Once a number has been submitted, and the value of the `input` is once again changed, the `button` should return to reading `Submit` .
```js
(() => {
const mockedComponent = Enzyme.mount(React.createElement(CheckUserAge));
const enter18AndClickButton = () => {
mockedComponent
.find('input')
.simulate('change', { target: { value: '18' } });
mockedComponent.find('button').simulate('click');
mockedComponent.update();
return mockedComponent.find('button').text();
};
const changeInputDontClickButton = () => {
mockedComponent
.find('input')
.simulate('change', { target: { value: '5' } });
mockedComponent.update();
return mockedComponent.find('button').text();
};
const enter10AndClickButton = () => {
mockedComponent
.find('input')
.simulate('change', { target: { value: '10' } });
mockedComponent.find('button').simulate('click');
mockedComponent.update();
return mockedComponent.find('button').text();
};
const userAge18 = enter18AndClickButton();
const changeInput1 = changeInputDontClickButton();
const userAge10 = enter10AndClickButton();
const changeInput2 = changeInputDontClickButton();
assert(
userAge18 === 'You May Enter' & &
changeInput1 === 'Submit' & &
userAge10 === 'You Shall Not Pass' & &
changeInput2 === 'Submit'
);
})();
2018-10-04 14:37:37 +01:00
```
2020-11-27 19:02:05 +01:00
Your code should not contain any `if/else` statements.
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
new RegExp(/(\s|;)if(\s|\()/).test(
Enzyme.mount(React.createElement(CheckUserAge)).instance().render.toString()
) === false
);
```
2020-09-03 01:24:22 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
## --after-user-code--
```jsx
ReactDOM.render(< CheckUserAge / > , document.getElementById('root'));
```
## --seed-contents--
2018-10-04 14:37:37 +01:00
```jsx
const inputStyle = {
width: 235,
margin: 5
2020-09-03 01:24:22 +01:00
};
2018-10-04 14:37:37 +01:00
class CheckUserAge extends React.Component {
constructor(props) {
super(props);
2020-09-15 09:53:25 -07:00
// Change code below this line
2018-10-04 14:37:37 +01:00
2020-09-15 09:53:25 -07:00
// Change code above this line
2018-10-04 14:37:37 +01:00
this.submit = this.submit.bind(this);
this.handleChange = this.handleChange.bind(this);
}
handleChange(e) {
this.setState({
input: e.target.value,
2018-10-20 21:02:47 +03:00
userAge: ''
2018-10-04 14:37:37 +01:00
});
}
submit() {
2019-09-24 16:12:50 +01:00
this.setState(state => ({
userAge: state.input
}));
2018-10-04 14:37:37 +01:00
}
render() {
const buttonOne = < button onClick = {this.submit} > Submit< / button > ;
const buttonTwo = < button > You May Enter< / button > ;
const buttonThree = < button > You Shall Not Pass< / button > ;
return (
< div >
< h3 > Enter Your Age to Continue< / h3 >
< input
style={inputStyle}
2020-09-03 01:24:22 +01:00
type='number'
2018-10-04 14:37:37 +01:00
value={this.state.input}
2020-09-03 01:24:22 +01:00
onChange={this.handleChange}
/>
< br / >
2020-09-15 09:53:25 -07:00
{/* Change code below this line */}
{/* Change code above this line */}
2018-10-04 14:37:37 +01:00
< / div >
);
}
2020-09-03 01:24:22 +01:00
}
2018-10-04 14:37:37 +01:00
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-10-04 14:37:37 +01:00
2020-07-13 18:58:50 +02:00
```jsx
2018-10-04 14:37:37 +01:00
const inputStyle = {
width: 235,
margin: 5
2020-09-03 01:24:22 +01:00
};
2018-10-04 14:37:37 +01:00
class CheckUserAge extends React.Component {
constructor(props) {
super(props);
this.state = {
2018-10-20 21:02:47 +03:00
userAge: '',
input: ''
2020-09-03 01:24:22 +01:00
};
2018-10-04 14:37:37 +01:00
this.submit = this.submit.bind(this);
this.handleChange = this.handleChange.bind(this);
}
handleChange(e) {
this.setState({
input: e.target.value,
2018-10-20 21:02:47 +03:00
userAge: ''
2018-10-04 14:37:37 +01:00
});
}
submit() {
2019-09-24 16:12:50 +01:00
this.setState(state => ({
userAge: state.input
}));
2018-10-04 14:37:37 +01:00
}
render() {
const buttonOne = < button onClick = {this.submit} > Submit< / button > ;
const buttonTwo = < button > You May Enter< / button > ;
const buttonThree = < button > You Shall Not Pass< / button > ;
return (
< div >
< h3 > Enter Your Age to Continue< / h3 >
< input
style={inputStyle}
2020-09-03 01:24:22 +01:00
type='number'
2018-10-04 14:37:37 +01:00
value={this.state.input}
2020-09-03 01:24:22 +01:00
onChange={this.handleChange}
/>
< br / >
{this.state.userAge === ''
? buttonOne
: this.state.userAge >= 18
? buttonTwo
: buttonThree}
2018-10-04 14:37:37 +01:00
< / div >
);
}
2020-09-03 01:24:22 +01:00
}
2018-10-04 14:37:37 +01:00
```