Files
freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/react/render-with-an-if-else-condition.chinese.md
Oliver Eyton-Williams 61460c8601 fix: insert blank line after ```
search and replace ```\n< with ```\n\n< to ensure there's an empty line
before closing tags
2020-08-16 04:45:20 +05:30

3.6 KiB
Raw Blame History

id, title, challengeType, isRequired, videoUrl, localeTitle
id title challengeType isRequired videoUrl localeTitle
5a24c314108439a4d4036184 Render with an If-Else Condition 6 false 使用If-Else条件渲染

Description

使用JavaScript控制渲染视图的另一个应用是将呈现的元素绑定到条件。当条件为真时一个视图呈现。当它是假的时它是一个不同的观点。您可以使用React组件的render()方法中的标准if/else语句执行此操作。

Instructions

MyComponent在其状态中包含一个boolean 用于跟踪是否要在UI中显示某个元素。该button切换此值的状态。目前它每次都呈现相同的UI。使用if/else语句重写render()方法,以便如果displaytrue ,则返回当前标记。否则,返回没有h1元素的标记。 注意:您必须编写if/else以传递测试。使用三元运算符不会通过此处。

Tests

tests:
  - text: <code>MyComponent</code>应该存在并呈现。
    testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); return mockedComponent.find('MyComponent').length === 1; })());
  - 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(mockedComponent.find(''div'').length === 1 && mockedComponent.find(''div'').children().length === 2 && mockedComponent.find(''button'').length === 1 && mockedComponent.find(''h1'').length === 1); }; '
  - 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(mockedComponent.find(''div'').length === 1 && mockedComponent.find(''div'').children().length === 1 && mockedComponent.find(''button'').length === 1 && mockedComponent.find(''h1'').length === 0); }; '
  - text: render方法应使用<code>if/else</code>语句来检查<code>this.state.display</code>的条件。
    testString: getUserInput => assert(getUserInput('index').includes('if') && getUserInput('index').includes('else'));

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

/section>