4.4 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	
			4.4 KiB
		
	
	
	
	
	
	
	
id, title, challengeType, isRequired, videoUrl, localeTitle
| id | title | challengeType | isRequired | videoUrl | localeTitle | 
|---|---|---|---|---|---|
| 5a24c314108439a4d4036185 | Use && for a More Concise Conditional | 6 | false | 使用&&获得更简洁的条件 | 
Description
else if语句来返回略有不同的UI,你可能会重复代码,这会留下错误的余地。相反,您可以使用&& logical运算符以更简洁的方式执行条件逻辑。这是可能的,因为您要检查条件是否为true ,如果是,则返回一些标记。下面是一个示例: {condition && <p>markup</p>}如果condition为true ,则返回标记。如果条件为false ,则在评估condition后操作将立即返回false并且不返回任何内容。您可以直接在JSX中包含这些语句,并在每个语句之后写入&&多个条件串在一起。这允许您在render()方法中处理更复杂的条件逻辑,而无需重复大量代码。 Instructions
h1仅在display为true呈现,但使用&& logical运算符而不是if/else语句。 Tests
tests:
  - text: <code>MyComponent</code>应该存在并呈现。
    testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); return mockedComponent.find("MyComponent").length; })(), "<code>MyComponent</code> should exist and render.");'
  - text: 当<code>display</code>设置为<code>true</code> ,应该渲染<code>div</code> , <code>button</code>和<code>h1</code> 。
    testString: '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 <code>display</code> is set to <code>true</code>, a <code>div</code>, <code>button</code>, and <code>h1</code> should render."); }; '
  - text: 当<code>display</code>设置为<code>false</code> ,只应呈现<code>div</code>和<code>button</code> 。
    testString: '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, "When <code>display</code> is set to <code>false</code>, only a <code>div</code> and <code>button</code> should render."); }; '
  - text: render方法应该使用&& logical运算符来检查this.state.display的条件。
    testString: 'getUserInput => assert(getUserInput("index").includes("&&"), "The render method should use the && logical operator to check the condition of this.state.display.");'
Challenge Seed
class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      display: true
    }
    this.toggleDisplay = this.toggleDisplay.bind(this);
  }
  toggleDisplay() {
    this.setState({
      display: !this.state.display
    });
  }
  render() {
    // change code below this line
    return (
       <div>
         <button onClick={this.toggleDisplay}>Toggle Display</button>
         <h1>Displayed!</h1>
       </div>
    );
  }
};
After Test
console.info('after the test');
Solution
// solution required