156 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			156 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| ---
 | |
| id: 5a24c314108439a4d4036184
 | |
| title: 使用 If-Else 条件进行渲染
 | |
| challengeType: 6
 | |
| forumTopicId: 301410
 | |
| dashedName: render-with-an-if-else-condition
 | |
| ---
 | |
| 
 | |
| # --description--
 | |
| 
 | |
| 使用 JavaScript 控制渲染视图的另一个应用是按条件渲染元素。 当条件为真时,将呈现一个视图, 反之,则呈现另一种视图。 可以在 React 组件的 `render()` 方法中使用的标准 `if/else` 语句来实现这一点。
 | |
| 
 | |
| # --instructions--
 | |
| 
 | |
| MyComponent 的 state 中包含一个 `boolean`(布尔值),用于跟踪是否要在 UI 中显示某个元素。 `button` 切换此值的状态。 目前,它每次都呈现相同的 UI。 用 `if/else` 语句重写 `render()` 方法,如果 `display` 为 `true` 则返回当前标记。 否则,返回不带 `h1` 元素的标记。
 | |
| 
 | |
| **注意:** 写 `if/else` 语句才能通过测试, 使用三元运算符是不会通过的。
 | |
| 
 | |
| # --hints--
 | |
| 
 | |
| `MyComponent` 应该存在并渲染。
 | |
| 
 | |
| ```js
 | |
| assert(
 | |
|   (function () {
 | |
|     const mockedComponent = Enzyme.mount(React.createElement(MyComponent));
 | |
|     return mockedComponent.find('MyComponent').length === 1;
 | |
|   })()
 | |
| );
 | |
| ```
 | |
| 
 | |
| 当 `display` 被设置为 `true` 时,`div`、`button` 和 `h1` 标签应该渲染。
 | |
| 
 | |
| ```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(
 | |
|     mockedComponent.find('div').length === 1 &&
 | |
|       mockedComponent.find('div').children().length === 2 &&
 | |
|       mockedComponent.find('button').length === 1 &&
 | |
|       mockedComponent.find('h1').length === 1
 | |
|   );
 | |
| };
 | |
| ```
 | |
| 
 | |
| 当 `display` 被设置为 `false` 时,只有 `div` 和 `button` 应该渲染。
 | |
| 
 | |
| ```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(
 | |
|     mockedComponent.find('div').length === 1 &&
 | |
|       mockedComponent.find('div').children().length === 1 &&
 | |
|       mockedComponent.find('button').length === 1 &&
 | |
|       mockedComponent.find('h1').length === 0
 | |
|   );
 | |
| };
 | |
| ```
 | |
| 
 | |
| Render 方法中应该使用 `if/else` 语句来检查 `this.state.display` 的条件。
 | |
| 
 | |
| ```js
 | |
| (getUserInput) =>
 | |
|   assert(
 | |
|     getUserInput('index').includes('if') &&
 | |
|       getUserInput('index').includes('else')
 | |
|   );
 | |
| ```
 | |
| 
 | |
| # --seed--
 | |
| 
 | |
| ## --after-user-code--
 | |
| 
 | |
| ```jsx
 | |
| ReactDOM.render(<MyComponent />, document.getElementById('root'))
 | |
| ```
 | |
| 
 | |
| ## --seed-contents--
 | |
| 
 | |
| ```jsx
 | |
| class MyComponent extends React.Component {
 | |
|   constructor(props) {
 | |
|     super(props);
 | |
|     this.state = {
 | |
|       display: true
 | |
|     }
 | |
|     this.toggleDisplay = this.toggleDisplay.bind(this);
 | |
|   }
 | |
|   toggleDisplay() {
 | |
|     this.setState((state) => ({
 | |
|       display: !state.display
 | |
|     }));
 | |
|   }
 | |
|   render() {
 | |
|     // Change code below this line
 | |
| 
 | |
|     return (
 | |
|        <div>
 | |
|          <button onClick={this.toggleDisplay}>Toggle Display</button>
 | |
|          <h1>Displayed!</h1>
 | |
|        </div>
 | |
|     );
 | |
|   }
 | |
| };
 | |
| ```
 | |
| 
 | |
| # --solutions--
 | |
| 
 | |
| ```jsx
 | |
| class MyComponent extends React.Component {
 | |
|   constructor(props) {
 | |
|     super(props);
 | |
|     this.state = {
 | |
|       display: true
 | |
|     }
 | |
|  this.toggleDisplay = this.toggleDisplay.bind(this);
 | |
|  }
 | |
|   toggleDisplay() {
 | |
|     this.setState((state) => ({
 | |
|       display: !state.display
 | |
|     }));
 | |
|   }
 | |
|   render() {
 | |
|     // Change code below this line
 | |
|     if (this.state.display) {
 | |
|       return (
 | |
|          <div>
 | |
|            <button onClick={this.toggleDisplay}>Toggle Display</button>
 | |
|            <h1>Displayed!</h1>
 | |
|          </div>
 | |
|       );
 | |
|     } else {
 | |
|       return (
 | |
|         <div>
 | |
|            <button onClick={this.toggleDisplay}>Toggle Display</button>
 | |
|          </div>
 | |
|       );
 | |
|     }
 | |
|   }
 | |
| };
 | |
| ```
 |