4.1 KiB
4.1 KiB
id | title | challengeType | isRequired | videoUrl | localeTitle |
---|---|---|---|---|---|
5a24c314108439a4d4036184 | Render with an If-Else Condition | 6 | false | 使用If-Else条件渲染 |
Description
render()
方法中的标准if/else
语句执行此操作。 Instructions
boolean
,用于跟踪是否要在UI中显示某个元素。该button
切换此值的状态。目前,它每次都呈现相同的UI。使用if/else
语句重写render()
方法,以便如果display
为true
,则返回当前标记。否则,返回没有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; })(), "<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(mockedComponent.find("div").length === 1 && mockedComponent.find("div").children().length === 2 && mockedComponent.find("button").length === 1 && mockedComponent.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(mockedComponent.find("div").length === 1 && mockedComponent.find("div").children().length === 1 && mockedComponent.find("button").length === 1 && mockedComponent.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方法应使用<code>if/else</code>语句来检查<code>this.state.display</code>的条件。
testString: 'getUserInput => assert(getUserInput("index").includes("if") && getUserInput("index").includes("else"), "The render method should use an <code>if/else</code> statement to check the condition of <code>this.state.display</code>.");'
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