--- id: 5a24c314108439a4d4036180 title: Optimize Re-Renders with shouldComponentUpdate challengeType: 6 isRequired: false videoUrl: '' localeTitle: 使用shouldComponentUpdate优化重新渲染 --- ## Description
到目前为止,如果任何组件接收到新的state或新的props ,它会重新呈现自己及其所有子项。这通常没问题。但是React提供了一个生命周期方法,您可以在子组件接收新stateprops时调用它,并特别声明组件是否应该更新。方法是shouldComponentUpdate() ,它将nextPropsnextState作为参数。此方法是优化性能的有用方法。例如,默认行为是您的组件在收到新props时重新渲染,即使props未更改。您可以使用shouldComponentUpdate()通过比较props来防止这种情况。该方法必须返回一个boolean值,告诉React是否更新组件。您可以将当前道具( this.props )与下一个道具( nextProps )进行比较,以确定是否需要更新,并相应地返回truefalse
## Instructions
shouldComponentUpdate()方法添加到名为OnlyEvens的组件中。目前,此方法返回true因此每次收到新props时, OnlyEvens重新渲染。修改方法,以便OnlyEvens更新仅当value的新道具的是偶数。单击“ Add按钮,在触发其他生命周期挂钩时,在浏览器控制台中查看事件的顺序。
## Tests
```yml tests: - text: Controller组件应将OnlyEvens组件呈现为OnlyEvens组件。 testString: 'assert((() => { const mockedComponent = Enzyme.mount(React.createElement(Controller)); return mockedComponent.find("Controller").length === 1 && mockedComponent.find("OnlyEvens").length === 1; })(), "The Controller component should render the OnlyEvens component as a child.");' - text: 应该在OnlyEvens组件上定义shouldComponentUpdate方法。 testString: 'assert((() => { const child = React.createElement(OnlyEvens).type.prototype.shouldComponentUpdate.toString().replace(/s/g,""); return child !== "undefined"; })(), "The shouldComponentUpdate method should be defined on the OnlyEvens component.");' - text: OnlyEvens组件应返回一个h1标记,该标记呈现this.props.value的值。 testString: 'async () => { const waitForIt = (fn) => new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250)); const mockedComponent = Enzyme.mount(React.createElement(Controller)); const first = () => { mockedComponent.setState({ value: 1000 }); return waitForIt(() => mockedComponent.find("h1").html()); }; const second = () => { mockedComponent.setState({ value: 10 }); return waitForIt(() => mockedComponent.find("h1").html()); }; const firstValue = await first(); const secondValue = await second(); assert(firstValue === "

1000

" && secondValue === "

10

", "The OnlyEvens component should return an h1 tag which renders the value of this.props.value."); }; ' - text: OnlyEvens只有在nextProps.value为偶数OnlyEvens应该重新渲染。 testString: 'async () => { const waitForIt = (fn) => new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250)); const mockedComponent = Enzyme.mount(React.createElement(Controller)); const first = () => { mockedComponent.setState({ value: 8 }); return waitForIt(() => mockedComponent.find("h1").text()); }; const second = () => { mockedComponent.setState({ value: 7 }); return waitForIt(() => mockedComponent.find("h1").text()); }; const third = () => { mockedComponent.setState({ value: 42 }); return waitForIt(() => mockedComponent.find("h1").text()); }; const firstValue = await first(); const secondValue = await second(); const thirdValue = await third(); assert(firstValue === "8" && secondValue === "8" && thirdValue === "42", "OnlyEvens should re-render only when nextProps.value is even."); }; ' ```
## Challenge Seed
```jsx class OnlyEvens extends React.Component { constructor(props) { super(props); } shouldComponentUpdate(nextProps, nextState) { console.log('Should I update?'); // change code below this line return true; // change code above this line } componentWillReceiveProps(nextProps) { console.log('Receiving new props...'); } componentDidUpdate() { console.log('Component re-rendered.'); } render() { return

{this.props.value}

} }; class Controller extends React.Component { constructor(props) { super(props); this.state = { value: 0 }; this.addValue = this.addValue.bind(this); } addValue() { this.setState({ value: this.state.value + 1 }); } render() { return (
); } }; ```
### After Test
```js console.info('after the test'); ```
## Solution
```js // solution required ```