2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 5a24c314108439a4d4036185
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Use & &  for a More Concise Conditional
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 6
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 301413
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: use--for-a-more-concise-conditional
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-25 09:19:24 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `if/else`  statements worked in the last challenge, but there's a more concise way to achieve the same result. Imagine that you are tracking several conditions in a component and you want different elements to render depending on each of these conditions. If you write a lot of `else if`  statements to return slightly different UIs, you may repeat code which leaves room for error. Instead, you can use the `&&`  logical operator to perform conditional logic in a more concise way. This is possible because you want to check if a condition is `true` , and if it is, return some markup. Here's an example:
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-03-19 00:40:32 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```jsx
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{condition & &  < p > markup< / p > }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If the `condition`  is `true` , the markup will be returned. If the condition is `false` , the operation will immediately return `false`  after evaluating the `condition`  and return nothing. You can include these statements directly in your JSX and string multiple conditions together by writing `&&`  after each one. This allows you to handle more complex conditional logic in your `render()`  method without repeating a lot of code.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --instructions--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Solve the previous example again, so the `h1`  only renders if `display`  is `true` , but use the `&&`  logical operator instead of an `if/else`  statement.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`MyComponent`  should exist and render. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const mockedComponent = Enzyme.mount(React.createElement(MyComponent));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return mockedComponent.find('MyComponent').length;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								When `display`  is set to `true` , a `div` , `button` , and `h1`  should render.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const waitForIt = (fn) =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const mockedComponent = Enzyme.mount(React.createElement(MyComponent));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const state_1 = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    mockedComponent.setState({ display: true });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return waitForIt(() => mockedComponent);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const updated = await state_1();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    updated.find('div').length === 1 & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      updated.find('div').children().length === 2 & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      updated.find('button').length === 1 & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      updated.find('h1').length === 1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								When `display`  is set to `false` , only a `div`  and `button`  should render.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const waitForIt = (fn) =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const mockedComponent = Enzyme.mount(React.createElement(MyComponent));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const state_1 = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    mockedComponent.setState({ display: false });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return waitForIt(() => mockedComponent);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const updated = await state_1();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    updated.find('div').length === 1 & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      updated.find('div').children().length === 1 & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      updated.find('button').length === 1 & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      updated.find('h1').length === 0
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-25 09:19:24 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The render method should use the `&&`  logical operator to check the condition of `this.state.display` .
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								(getUserInput) => assert(getUserInput('index').includes('&&')); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## --after-user-code--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```jsx
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								ReactDOM.render(< MyComponent  / > , document.getElementById('root'))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## --seed-contents--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```jsx
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class MyComponent extends React.Component {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  constructor(props) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    super(props);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.state = {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      display: true
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.toggleDisplay = this.toggleDisplay.bind(this);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  toggleDisplay() {
							 
						 
					
						
							
								
									
										
										
										
											2019-09-24 16:12:50 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    this.setState(state => ({
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      display: !state.display
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }));
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  render() {
							 
						 
					
						
							
								
									
										
										
										
											2020-09-15 09:53:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // Change code below this line
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    return (
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								       < div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								         < button  onClick = {this.toggleDisplay} > Toggle Display< / button > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								         < h1 > Displayed!< / h1 > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								       < / div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-13 18:58:50 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```jsx
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								class MyComponent extends React.Component {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  constructor(props) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    super(props);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.state = {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      display: true
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2018-10-08 01:01:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								 this.toggleDisplay = this.toggleDisplay.bind(this);
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  toggleDisplay() {
							 
						 
					
						
							
								
									
										
										
										
											2019-09-24 16:12:50 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    this.setState(state => ({
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      display: !state.display
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }));
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  render() {
							 
						 
					
						
							
								
									
										
										
										
											2020-09-15 09:53:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // Change code below this line
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    return (
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								       < div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								         < button  onClick = {this.toggleDisplay} > Toggle Display< / button > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								         {this.state.display & &  < h1 > Displayed!< / h1 > }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								       < / div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```