React: Optimize Re-Renders with shouldComponentUpdate

Tell us what’s happening:

Your code so far


class OnlyEvens extends React.Component {
constructor(props) {
  super(props);
}
shouldComponentUpdate(nextProps, nextState) {
  console.log('Should I update?');
  // Change code below this line
  if(this.props===this.nextProps) {
  return true
  };
  // Change code above this line
}
componentDidUpdate() {
  console.log('Component re-rendered.');
}
render() {
  return <h1>{this.props.value}</h1>;
}
}

class Controller extends React.Component {
constructor(props) {
  super(props);
  this.state = {
    value: 0
  };
  this.addValue = this.addValue.bind(this);
}
addValue() {
  this.setState(state => ({
    value: state.value + 1
  }));
}
render() {
  return (
    <div>
      <button onClick={this.addValue}>Add</button>
      <OnlyEvens value={this.state.value} />
    </div>
  );
}
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36.

Challenge: Optimize Re-Renders with shouldComponentUpdate

Link to the challenge:

Hey, Alex.

Do you have a question?

If so, please edit your post to include it in the Tell us what’s happening section.

The more information you give us, the more likely we are to be able to help.